<!DOCTYPE html>
<html >

<head>

  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Python3学习教程</title>
  <meta name="description" content="Python3学习教程">
  <meta name="generator" content="bookdown 0.5 and GitBook 2.6.7">

  <meta property="og:title" content="Python3学习教程" />
  <meta property="og:type" content="book" />
  
  
  
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="Python3学习教程" />
  
  
  

<meta name="author" content="易生信培训团队">
<meta name="author" content="联系我们: train@ehbio.com">
<meta name="author" content="抱歉，转成PDF后部分格式问题还未解决，会继续更新调整">


<meta name="date" content="2018-05-09">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  
  
<link rel="prev" href="index.html">
<link rel="next" href="py3-ct.html">
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />









<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
  margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>

<link rel="stylesheet" href="style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="http://www.ehbio.com"><img src="http://www.ehbio.com/logos/ehbio_gitbook_logo.png" width="95%"></a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>EHBIO Gene Technology</a></li>
<li class="chapter" data-level="1" data-path="pythonbasic.html"><a href="pythonbasic.html"><i class="fa fa-check"></i><b>1</b> Python基础</a><ul>
<li class="chapter" data-level="1.1" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.1"><i class="fa fa-check"></i><b>1.1</b> 交互模式下表达式</a></li>
<li class="chapter" data-level="1.2" data-path="pythonbasic.html"><a href="pythonbasic.html#pythonintfloatstr"><i class="fa fa-check"></i><b>1.2</b> Python中的数据类型：整数（int）、浮点（float）和字符串（str）</a></li>
<li class="chapter" data-level="1.3" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.3"><i class="fa fa-check"></i><b>1.3</b> 字符串的连接和复制</a></li>
<li class="chapter" data-level="1.4" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.4"><i class="fa fa-check"></i><b>1.4</b> 变量</a></li>
<li class="chapter" data-level="1.5" data-path="pythonbasic.html"><a href="pythonbasic.html#helloworld.py"><i class="fa fa-check"></i><b>1.5</b> 第一小程序HelloWorld.py</a></li>
<li class="chapter" data-level="1.6" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.6"><i class="fa fa-check"></i><b>1.6</b> 逻辑和比较操作</a><ul>
<li class="chapter" data-level="1.6.1" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.6.1"><i class="fa fa-check"></i><b>1.6.1</b> 布尔逻辑值</a></li>
<li class="chapter" data-level="1.6.2" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.6.2"><i class="fa fa-check"></i><b>1.6.2</b> 比较操作符</a></li>
<li class="chapter" data-level="1.6.3" data-path="pythonbasic.html"><a href="pythonbasic.html#-and-or-not"><i class="fa fa-check"></i><b>1.6.3</b> 布尔操作符 and or not</a></li>
<li class="chapter" data-level="1.6.4" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.6.4"><i class="fa fa-check"></i><b>1.6.4</b> 布尔操作和比较操作符</a></li>
</ul></li>
<li class="chapter" data-level="1.7" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.7"><i class="fa fa-check"></i><b>1.7</b> 控制流</a><ul>
<li class="chapter" data-level="1.7.1" data-path="pythonbasic.html"><a href="pythonbasic.html#if-"><i class="fa fa-check"></i><b>1.7.1</b> if 语句</a></li>
<li class="chapter" data-level="1.7.2" data-path="pythonbasic.html"><a href="pythonbasic.html#elif-"><i class="fa fa-check"></i><b>1.7.2</b> elif 否则如果</a></li>
<li class="chapter" data-level="1.7.3" data-path="pythonbasic.html"><a href="pythonbasic.html#while"><i class="fa fa-check"></i><b>1.7.3</b> while循环</a></li>
<li class="chapter" data-level="1.7.4" data-path="pythonbasic.html"><a href="pythonbasic.html#breakcontinue"><i class="fa fa-check"></i><b>1.7.4</b> break和continue</a></li>
<li class="chapter" data-level="1.7.5" data-path="pythonbasic.html"><a href="pythonbasic.html#for--range"><i class="fa fa-check"></i><b>1.7.5</b> for 和 range（）函数</a></li>
<li class="chapter" data-level="1.7.6" data-path="pythonbasic.html"><a href="pythonbasic.html#range"><i class="fa fa-check"></i><b>1.7.6</b> range()函数（开始，停止，步长）</a></li>
</ul></li>
<li class="chapter" data-level="1.8" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.8"><i class="fa fa-check"></i><b>1.8</b> 导入模块</a></li>
<li class="chapter" data-level="1.9" data-path="pythonbasic.html"><a href="pythonbasic.html#-"><i class="fa fa-check"></i><b>1.9</b> 函数： 内置函数、自定义函数</a><ul>
<li class="chapter" data-level="1.9.1" data-path="pythonbasic.html"><a href="pythonbasic.html#printlen-input-intstr"><i class="fa fa-check"></i><b>1.9.1</b> 函数print（），len （）,input （），int（），str（）均为内置函数</a></li>
</ul></li>
<li class="chapter" data-level="1.10" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.10"><i class="fa fa-check"></i><b>1.10</b> 局部和全局作用域</a></li>
<li class="chapter" data-level="1.11" data-path="pythonbasic.html"><a href="pythonbasic.html#global"><i class="fa fa-check"></i><b>1.11</b> 声明为全局变量global</a></li>
<li class="chapter" data-level="1.12" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.12"><i class="fa fa-check"></i><b>1.12</b> 异常的处理</a></li>
<li class="chapter" data-level="1.13" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.13"><i class="fa fa-check"></i><b>1.13</b> 列表</a></li>
<li class="chapter" data-level="1.14" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.14"><i class="fa fa-check"></i><b>1.14</b> 字符串和元组</a></li>
<li class="chapter" data-level="1.15" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.15"><i class="fa fa-check"></i><b>1.15</b> 引用</a></li>
<li class="chapter" data-level="1.16" data-path="pythonbasic.html"><a href="pythonbasic.html#--"><i class="fa fa-check"></i><b>1.16</b> 字典 键：值 对</a></li>
<li class="chapter" data-level="1.17" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.17"><i class="fa fa-check"></i><b>1.17</b> 字典与列表</a><ul>
<li class="chapter" data-level="1.17.1" data-path="pythonbasic.html"><a href="pythonbasic.html#keysvaluesitems"><i class="fa fa-check"></i><b>1.17.1</b> keys()、values（）和items( )</a></li>
</ul></li>
<li class="chapter" data-level="1.18" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.18"><i class="fa fa-check"></i><b>1.18</b> 字符串操作</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="py3-ct.html"><a href="py3-ct.html"><i class="fa fa-check"></i><b>2</b> Python 教程</a><ul>
<li class="chapter" data-level="2.1" data-path="py3-ct.html"><a href="py3-ct.html#section-2.1"><i class="fa fa-check"></i><b>2.1</b> 背景介绍</a><ul>
<li class="chapter" data-level="2.1.1" data-path="py3-ct.html"><a href="py3-ct.html#section-2.1.1"><i class="fa fa-check"></i><b>2.1.1</b> 编程开篇</a></li>
<li class="chapter" data-level="2.1.2" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.2</b> 为什么学习Python</a></li>
<li class="chapter" data-level="2.1.3" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.3</b> Python常用包</a></li>
<li class="chapter" data-level="2.1.4" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.4</b> 怎么学习Python</a></li>
<li class="chapter" data-level="2.1.5" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.5</b> Python学习的几个阶段</a></li>
<li class="chapter" data-level="2.1.6" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.6</b> 如何安装Python</a></li>
<li class="chapter" data-level="2.1.7" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.7</b> 如何运行Python命令和脚本</a></li>
<li class="chapter" data-level="2.1.8" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.8</b> 使用什么编辑器写Python脚本</a></li>
</ul></li>
<li class="chapter" data-level="2.2" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.2</b> Python程序事例</a></li>
<li class="chapter" data-level="2.3" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.3</b> Python语法</a><ul>
<li class="chapter" data-level="2.3.1" data-path="py3-ct.html"><a href="py3-ct.html#section-2.3.1"><i class="fa fa-check"></i><b>2.3.1</b> 层级缩进</a></li>
<li class="chapter" data-level="2.3.2" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.3.2</b> Python作为计算器的使用</a></li>
<li class="chapter" data-level="2.3.3" data-path="py3-ct.html"><a href="py3-ct.html#section-2.3.3"><i class="fa fa-check"></i><b>2.3.3</b> 变量、数据结构、流程控制</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="py3-ct.html"><a href="py3-ct.html#section-2.4"><i class="fa fa-check"></i><b>2.4</b> 输入输出</a><ul>
<li class="chapter" data-level="2.4.1" data-path="py3-ct.html"><a href="py3-ct.html#section-2.4.1"><i class="fa fa-check"></i><b>2.4.1</b> 交互式输入输出</a></li>
<li class="chapter" data-level="2.4.2" data-path="py3-ct.html"><a href="py3-ct.html#section-2.4.2"><i class="fa fa-check"></i><b>2.4.2</b> 文件读写</a></li>
</ul></li>
<li class="chapter" data-level="2.5" data-path="py3-ct.html"><a href="py3-ct.html#section-2.5"><i class="fa fa-check"></i><b>2.5</b> 实战练习（一）</a><ul>
<li class="chapter" data-level="2.5.1" data-path="py3-ct.html"><a href="py3-ct.html#section-2.5.1"><i class="fa fa-check"></i><b>2.5.1</b> 背景知识</a></li>
<li class="chapter" data-level="2.5.2" data-path="pythonbasic.html"><a href="pythonbasic.html#-"><i class="fa fa-check"></i><b>2.5.2</b> 作业 (一)</a></li>
</ul></li>
<li class="chapter" data-level="2.6" data-path="py3-ct.html"><a href="py3-ct.html#section-2.6"><i class="fa fa-check"></i><b>2.6</b> 函数操作</a><ul>
<li class="chapter" data-level="2.6.1" data-path="py3-ct.html"><a href="py3-ct.html#section-2.6.1"><i class="fa fa-check"></i><b>2.6.1</b> 作业（二）</a></li>
</ul></li>
<li class="chapter" data-level="2.7" data-path="py3-ct.html"><a href="py3-ct.html#section-2.7"><i class="fa fa-check"></i><b>2.7</b> 模块</a></li>
<li class="chapter" data-level="2.8" data-path="py3-ct.html"><a href="py3-ct.html#section-2.8"><i class="fa fa-check"></i><b>2.8</b> 命令行参数</a><ul>
<li class="chapter" data-level="2.8.1" data-path="py3-ct.html"><a href="py3-ct.html#section-2.8.1"><i class="fa fa-check"></i><b>2.8.1</b> 作业（三）</a></li>
</ul></li>
<li class="chapter" data-level="2.9" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.9</b> 更多Python内容</a><ul>
<li class="chapter" data-level="2.9.1" data-path="py3-ct.html"><a href="py3-ct.html#section-2.9.1"><i class="fa fa-check"></i><b>2.9.1</b> <strong>单语句块</strong></a></li>
<li class="chapter" data-level="2.9.2" data-path="py3-ct.html"><a href="py3-ct.html#section-2.9.2"><i class="fa fa-check"></i><b>2.9.2</b> 列表解析</a></li>
<li class="chapter" data-level="2.9.3" data-path="py3-ct.html"><a href="py3-ct.html#section-2.9.3"><i class="fa fa-check"></i><b>2.9.3</b> 字典解析</a></li>
<li class="chapter" data-level="2.9.4" data-path="py3-ct.html"><a href="py3-ct.html#section-2.9.4"><i class="fa fa-check"></i><b>2.9.4</b> 断言</a></li>
<li class="chapter" data-level="2.9.5" data-path="py3-ct.html"><a href="py3-ct.html#section-2.9.5"><i class="fa fa-check"></i><b>2.9.5</b> 更多字符串方法</a></li>
<li class="chapter" data-level="2.9.6" data-path="py3-ct.html"><a href="py3-ct.html#lambda-map-filer-reduce-"><i class="fa fa-check"></i><b>2.9.6</b> lambda, map, filer, reduce (保留节目)</a></li>
<li class="chapter" data-level="2.9.7" data-path="py3-ct.html"><a href="py3-ct.html#exec-eval-python-"><i class="fa fa-check"></i><b>2.9.7</b> <strong>exec, eval (执行字符串python语句, 保留节目)</strong></a></li>
<li class="chapter" data-level="2.9.8" data-path="py3-ct.html"><a href="py3-ct.html#section-2.9.8"><i class="fa fa-check"></i><b>2.9.8</b> 正则表达式</a></li>
</ul></li>
<li class="chapter" data-level="2.10" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.10</b> Python画图</a></li>
<li class="chapter" data-level="2.11" data-path="py3-ct.html"><a href="py3-ct.html#reference"><i class="fa fa-check"></i><b>2.11</b> Reference</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="Python-plot-WXN.html"><a href="Python-plot-WXN.html"><i class="fa fa-check"></i><b>3</b> Python作图</a><ul>
<li class="chapter" data-level="3.1" data-path="Python-plot-WXN.html"><a href="Python-plot-WXN.html#section-3.1"><i class="fa fa-check"></i><b>3.1</b> 绘图基础</a><ul>
<li class="chapter" data-level="3.1.1" data-path="Python-plot-WXN.html"><a href="Python-plot-WXN.html#figuresubplot-1"><i class="fa fa-check"></i><b>3.1.1</b> Figure和Subplot</a></li>
<li class="chapter" data-level="3.1.2" data-path="Python-plot-WXN.html"><a href="Python-plot-WXN.html#subplot-1"><i class="fa fa-check"></i><b>3.1.2</b> 调整subplot周围间距</a></li>
<li class="chapter" data-level="3.1.3" data-path="Python-plot-WXN.html"><a href="Python-plot-WXN.html#--1"><i class="fa fa-check"></i><b>3.1.3</b> 颜色 标记和线型</a></li>
<li class="chapter" data-level="3.1.4" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.1.4</b> 刻度、标签和图例</a></li>
<li class="chapter" data-level="3.1.5" data-path="Python-plot-WXN.html"><a href="Python-plot-WXN.html#legend-1"><i class="fa fa-check"></i><b>3.1.5</b> 添加图例legend</a></li>
<li class="chapter" data-level="3.1.6" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.1.6</b> 注解</a></li>
<li class="chapter" data-level="3.1.7" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.1.7</b> 图片保存</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="Python-plot-WXN.html"><a href="Python-plot-WXN.html#section-3.2"><i class="fa fa-check"></i><b>3.2</b> 绘图实例</a><ul>
<li class="chapter" data-level="3.2.1" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.2.1</b> 绘制散点图</a></li>
<li class="chapter" data-level="3.2.2" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.2.2</b> 折线图</a></li>
<li class="chapter" data-level="3.2.3" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.2.3</b> 直方图</a></li>
<li class="chapter" data-level="3.2.4" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.2.4</b> 直条图</a></li>
<li class="chapter" data-level="3.2.5" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.2.5</b> 箱线图</a></li>
<li class="chapter" data-level="3.2.6" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.2.6</b> 饼图</a></li>
<li class="chapter" data-level="3.2.7" data-path="Python-plot-WXN.html"><a href="Python-plot-WXN.html#section-3.2.7"><i class="fa fa-check"></i><b>3.2.7</b> 绘制基因矩阵的热图</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="Py3-pratcise-ct.html"><a href="Py3-pratcise-ct.html"><i class="fa fa-check"></i><b>4</b> Python实战</a><ul>
<li class="chapter" data-level="4.1" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>4.1</b> Python实战</a><ul>
<li class="chapter" data-level="4.1.1" data-path="Py3-pratcise-ct.html"><a href="Py3-pratcise-ct.html#id"><i class="fa fa-check"></i><b>4.1.1</b> ID转换</a></li>
<li class="chapter" data-level="4.1.2" data-path="Py3-pratcise-ct.html"><a href="Py3-pratcise-ct.html#section-4.1.2"><i class="fa fa-check"></i><b>4.1.2</b> 每条染色体基因数目统计</a></li>
<li class="chapter" data-level="4.1.3" data-path="Py3-pratcise-ct.html"><a href="Py3-pratcise-ct.html#section-4.1.3"><i class="fa fa-check"></i><b>4.1.3</b> 所有外显子总长度统计</a></li>
</ul></li>
<li class="chapter" data-level="4.2" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>4.2</b> Python小技巧</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html"><i class="fa fa-check"></i><b>5</b> Pandas 学习教程</a><ul>
<li class="chapter" data-level="5.1" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#what-is-pandas"><i class="fa fa-check"></i><b>5.1</b> What is pandas</a></li>
<li class="chapter" data-level="5.2" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#pandas"><i class="fa fa-check"></i><b>5.2</b> Pandas读取文件</a><ul>
<li class="chapter" data-level="5.2.1" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.1"><i class="fa fa-check"></i><b>5.2.1</b> 获取目标文件</a></li>
<li class="chapter" data-level="5.2.2" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.2"><i class="fa fa-check"></i><b>5.2.2</b> 查看目标文件内容和格式</a></li>
<li class="chapter" data-level="5.2.3" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.3"><i class="fa fa-check"></i><b>5.2.3</b> 读取两列文件</a></li>
<li class="chapter" data-level="5.2.4" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.4"><i class="fa fa-check"></i><b>5.2.4</b> 数据表的索引</a></li>
<li class="chapter" data-level="5.2.5" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.5"><i class="fa fa-check"></i><b>5.2.5</b> 读取多列文件</a></li>
<li class="chapter" data-level="5.2.6" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.6"><i class="fa fa-check"></i><b>5.2.6</b> 选取多列数据</a></li>
<li class="chapter" data-level="5.2.7" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.7"><i class="fa fa-check"></i><b>5.2.7</b> 重命名列名字</a></li>
<li class="chapter" data-level="5.2.8" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.8"><i class="fa fa-check"></i><b>5.2.8</b> 合并矩阵</a></li>
<li class="chapter" data-level="5.2.9" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.9"><i class="fa fa-check"></i><b>5.2.9</b> 矩阵数据提取</a></li>
<li class="chapter" data-level="5.2.10" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#meta-data"><i class="fa fa-check"></i><b>5.2.10</b> 读取META data文件</a></li>
</ul></li>
<li class="chapter" data-level="5.3" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#pandas"><i class="fa fa-check"></i><b>5.3</b> Pandas写入文件</a><ul>
<li class="chapter" data-level="5.3.1" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.3.1"><i class="fa fa-check"></i><b>5.3.1</b> 写入文本文件</a></li>
</ul></li>
<li class="chapter" data-level="5.4" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#pandas"><i class="fa fa-check"></i><b>5.4</b> PANDAS矩阵的小应用</a></li>
<li class="chapter" data-level="5.5" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#seaborn"><i class="fa fa-check"></i><b>5.5</b> Seaborn绘图</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="numpy-scipy.html"><a href="numpy-scipy.html"><i class="fa fa-check"></i><b>6</b> Python科学计算</a><ul>
<li class="chapter" data-level="6.1" data-path="numpy-scipy.html"><a href="numpy-scipy.html#numpy"><i class="fa fa-check"></i><b>6.1</b> NumPy</a><ul>
<li class="chapter" data-level="6.1.1" data-path="numpy-scipy.html"><a href="numpy-scipy.html#numpy"><i class="fa fa-check"></i><b>6.1.1</b> NumPy数组</a></li>
<li class="chapter" data-level="6.1.2" data-path="numpy-scipy.html"><a href="numpy-scipy.html#numpy"><i class="fa fa-check"></i><b>6.1.2</b> 布尔语句和NumPy数组</a></li>
<li class="chapter" data-level="6.1.3" data-path="numpy-scipy.html"><a href="numpy-scipy.html#numpy"><i class="fa fa-check"></i><b>6.1.3</b> NumPy读写文件</a></li>
<li class="chapter" data-level="6.1.4" data-path="numpy-scipy.html"><a href="numpy-scipy.html#numpymath"><i class="fa fa-check"></i><b>6.1.4</b> NumPy的Math模块</a></li>
</ul></li>
<li class="chapter" data-level="6.2" data-path="numpy-scipy.html"><a href="numpy-scipy.html#scipy"><i class="fa fa-check"></i><b>6.2</b> SciPy</a><ul>
<li class="chapter" data-level="6.2.1" data-path="numpy-scipy.html"><a href="numpy-scipy.html#section-6.2.1"><i class="fa fa-check"></i><b>6.2.1</b> 最优化和最小化</a></li>
<li class="chapter" data-level="6.2.2" data-path="numpy-scipy.html"><a href="numpy-scipy.html#section-6.2.2"><i class="fa fa-check"></i><b>6.2.2</b> 插值</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="7" data-path="Py3-test.html"><a href="Py3-test.html"><i class="fa fa-check"></i><b>7</b> 易生信Python培训练习和考核题目</a></li>
<li class="chapter" data-level="8" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i><b>8</b> 生信教程文章集锦</a><ul>
<li class="chapter" data-level="8.1" data-path="references.html"><a href="references.html#section-8.1"><i class="fa fa-check"></i><b>8.1</b> 生信宝典</a><ul>
<li class="chapter" data-level="8.1.1" data-path="references.html"><a href="references.html#section-8.1.1"><i class="fa fa-check"></i><b>8.1.1</b> 系列教程</a></li>
<li class="chapter" data-level="8.1.2" data-path="references.html"><a href="references.html#ngs"><i class="fa fa-check"></i><b>8.1.2</b> NGS分析工具评估</a></li>
<li class="chapter" data-level="8.1.3" data-path="references.html"><a href="references.html#section-8.1.3"><i class="fa fa-check"></i><b>8.1.3</b> 宏基因组教程</a></li>
<li class="chapter" data-level="8.1.4" data-path="references.html"><a href="references.html#section-8.1.4"><i class="fa fa-check"></i><b>8.1.4</b> 系列宣传</a></li>
<li class="chapter" data-level="8.1.5" data-path="references.html"><a href="references.html#section-8.1.5"><i class="fa fa-check"></i><b>8.1.5</b> 生信生物知识</a></li>
<li class="chapter" data-level="8.1.6" data-path="references.html"><a href="references.html#section-8.1.6"><i class="fa fa-check"></i><b>8.1.6</b> 文献精读</a></li>
<li class="chapter" data-level="8.1.7" data-path="references.html"><a href="references.html#linux"><i class="fa fa-check"></i><b>8.1.7</b> Linux</a></li>
<li class="chapter" data-level="8.1.8" data-path="references.html"><a href="references.html#circos"><i class="fa fa-check"></i><b>8.1.8</b> CIRCOS系列</a></li>
<li class="chapter" data-level="8.1.9" data-path="references.html"><a href="references.html#r"><i class="fa fa-check"></i><b>8.1.9</b> R统计和作图</a></li>
<li class="chapter" data-level="8.1.10" data-path="references.html"><a href="references.html#section-8.1.10"><i class="fa fa-check"></i><b>8.1.10</b> 扩增子三步曲</a></li>
<li class="chapter" data-level="8.1.11" data-path="references.html"><a href="references.html#section-8.1.11"><i class="fa fa-check"></i><b>8.1.11</b> 宏基因组分析专题</a></li>
<li class="chapter" data-level="8.1.12" data-path="references.html"><a href="references.html#ngs"><i class="fa fa-check"></i><b>8.1.12</b> NGS基础</a></li>
<li class="chapter" data-level="8.1.13" data-path="references.html"><a href="references.html#section-8.1.13"><i class="fa fa-check"></i><b>8.1.13</b> 癌症数据库</a></li>
<li class="chapter" data-level="8.1.14" data-path="references.html"><a href="references.html#python-1"><i class="fa fa-check"></i><b>8.1.14</b> Python</a></li>
<li class="chapter" data-level="8.1.15" data-path="references.html"><a href="references.html#ngs"><i class="fa fa-check"></i><b>8.1.15</b> NGS软件</a></li>
<li class="chapter" data-level="8.1.16" data-path="references.html"><a href="references.html#cytoscape"><i class="fa fa-check"></i><b>8.1.16</b> Cytoscape网络图</a></li>
<li class="chapter" data-level="8.1.17" data-path="references.html"><a href="references.html#section-8.1.17"><i class="fa fa-check"></i><b>8.1.17</b> 分子对接</a></li>
<li class="chapter" data-level="8.1.18" data-path="references.html"><a href="references.html#section-8.1.18"><i class="fa fa-check"></i><b>8.1.18</b> 生信宝典之傻瓜式</a></li>
<li class="chapter" data-level="8.1.19" data-path="references.html"><a href="references.html#section-8.1.19"><i class="fa fa-check"></i><b>8.1.19</b> 生信人写程序</a></li>
<li class="chapter" data-level="8.1.20" data-path="references.html"><a href="references.html#section-8.1.20"><i class="fa fa-check"></i><b>8.1.20</b> 小技巧系列</a></li>
<li class="chapter" data-level="8.1.21" data-path="references.html"><a href="references.html#section-8.1.21"><i class="fa fa-check"></i><b>8.1.21</b> 招聘</a></li>
</ul></li>
<li class="chapter" data-level="8.2" data-path="references.html"><a href="references.html#section-8.2"><i class="fa fa-check"></i><b>8.2</b> 宏基因组</a><ul>
<li class="chapter" data-level="8.2.1" data-path="references.html"><a href="references.html#section-8.2.1"><i class="fa fa-check"></i><b>8.2.1</b> 精选文章推荐</a></li>
<li class="chapter" data-level="8.2.2" data-path="references.html"><a href="references.html#section-8.2.2"><i class="fa fa-check"></i><b>8.2.2</b> 培训、会议、征稿、招聘</a></li>
<li class="chapter" data-level="8.2.3" data-path="references.html"><a href="references.html#section-8.2.3"><i class="fa fa-check"></i><b>8.2.3</b> 科研经验</a></li>
<li class="chapter" data-level="8.2.4" data-path="references.html"><a href="references.html#section-8.2.4"><i class="fa fa-check"></i><b>8.2.4</b> 软件和数据库使用</a></li>
<li class="chapter" data-level="8.2.5" data-path="references.html"><a href="references.html#section-8.2.5"><i class="fa fa-check"></i><b>8.2.5</b> 扩增子学习三步曲</a></li>
<li class="chapter" data-level="8.2.6" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>8.2.6</b> 宏基因组分析专题</a></li>
<li class="chapter" data-level="8.2.7" data-path="references.html"><a href="references.html#r"><i class="fa fa-check"></i><b>8.2.7</b> R统计绘图</a></li>
<li class="chapter" data-level="8.2.8" data-path="references.html"><a href="references.html#section-8.2.8"><i class="fa fa-check"></i><b>8.2.8</b> 实验设计与技术</a></li>
<li class="chapter" data-level="8.2.9" data-path="references.html"><a href="references.html#section-8.2.9"><i class="fa fa-check"></i><b>8.2.9</b> 基础知识</a></li>
<li class="chapter" data-level="8.2.10" data-path="references.html"><a href="references.html#section-8.2.10"><i class="fa fa-check"></i><b>8.2.10</b> 必读综述</a></li>
<li class="chapter" data-level="8.2.11" data-path="references.html"><a href="references.html#section-8.2.11"><i class="fa fa-check"></i><b>8.2.11</b> 高分文章套路解读</a></li>
<li class="chapter" data-level="8.2.12" data-path="pythonbasic.html"><a href="pythonbasic.html#-"><i class="fa fa-check"></i><b>8.2.12</b> 科普视频-寓教于乐</a></li>
<li class="chapter" data-level="8.2.13" data-path="references.html"><a href="references.html#section-8.2.13"><i class="fa fa-check"></i><b>8.2.13</b> 友军文章汇总推荐</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="9" data-path="company-intro.html"><a href="company-intro.html"><i class="fa fa-check"></i><b>9</b> 公司简介</a></li>
<li class="divider"></li>
<li><a href="mailto:ct@ehbio.com" target="blank">ct@ehbio.com</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Python3学习教程</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="pythonbasic" class="section level1">
<h1><span class="header-section-number">1</span> Python基础</h1>
<p>王绪宁制作</p>
<p>欢迎访问我们的视频课程 <a href="https://bioinfo.ke.qq.com" class="uri">https://bioinfo.ke.qq.com</a>。</p>
<div id="section-1.1" class="section level2">
<h2><span class="header-section-number">1.1</span> 交互模式下表达式</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#表达式： 值、操作符优先级</span>
<span class="dv">2</span> <span class="op">+</span> <span class="dv">2</span> </code></pre></div>
<pre><code>4</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="dv">2</span> <span class="op">+</span> <span class="dv">3</span> <span class="op">*</span> <span class="dv">5</span></code></pre></div>
<pre><code>17</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">( <span class="dv">2</span> <span class="op">+</span> <span class="dv">3</span> ) <span class="op">*</span> <span class="dv">5</span></code></pre></div>
<pre><code>25</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="dv">23</span> <span class="op">%</span> <span class="dv">7</span> <span class="co">#取余数，判断数据是奇数还是偶数时应用</span></code></pre></div>
<pre><code>2</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="dv">23</span> <span class="op">//</span> <span class="dv">7</span> <span class="co">#</span></code></pre></div>
<pre><code>3</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#示例报错，语法要符合规则，像英语语法规则一样。</span>
<span class="dv">2</span><span class="op">+</span></code></pre></div>
<pre><code>  File &quot;&lt;ipython-input-9-d066253e063d&gt;&quot;, line 2
    2+
      ^
SyntaxError: invalid syntax</code></pre>
</div>
<div id="pythonintfloatstr" class="section level2">
<h2><span class="header-section-number">1.2</span> Python中的数据类型：整数（int）、浮点（float）和字符串（str）</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">type</span>(<span class="dv">2</span>)</code></pre></div>
<pre><code>int</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">type</span>(<span class="fl">2.5</span>)</code></pre></div>
<pre><code>float</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">int</span>(<span class="fl">2.4</span>)</code></pre></div>
<pre><code>2</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">type</span>(<span class="st">&#39;Hello, world！&#39;</span>) <span class="co">#字符串用成对的引号（单引号、双引号、三引号）</span></code></pre></div>
<pre><code>str</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#如果字符串没有引号，则报错</span>
‘hello，world</code></pre></div>
<pre><code>  File &quot;&lt;ipython-input-16-874a8e2fccea&gt;&quot;, line 2
    ‘hello，world
               ^
SyntaxError: invalid character in identifier</code></pre>
</div>
<div id="section-1.3" class="section level2">
<h2><span class="header-section-number">1.3</span> 字符串的连接和复制</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;Hello&#39;</span> <span class="op">+</span> <span class="st">&#39;world!&#39;</span></code></pre></div>
<pre><code>&#39;helloworld!&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;hello&#39;</span> <span class="op">+</span> <span class="dv">21</span> <span class="co">#错误示例，两个字符串才能相加</span></code></pre></div>
<pre><code>---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

&lt;ipython-input-18-2f37c7cd8411&gt; in &lt;module&gt;()
----&gt; 1 &#39;hello&#39;+21


TypeError: must be str, not int</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;hello&#39;</span> <span class="op">+</span> <span class="st">&#39;21&#39;</span></code></pre></div>
<pre><code>&#39;hello21&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;Hello&#39;</span> <span class="op">*</span> <span class="dv">5</span> <span class="co">#必须是整数,才可复制</span></code></pre></div>
<pre><code>&#39;HelloHelloHelloHelloHello&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;hello&#39;</span><span class="op">*</span><span class="fl">5.0</span> <span class="co">#错误示例</span></code></pre></div>
<pre><code>---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

&lt;ipython-input-21-8dc053398ad8&gt; in &lt;module&gt;()
----&gt; 1 &#39;hello&#39;*5.0


TypeError: can&#39;t multiply sequence by non-int of type &#39;float&#39;</code></pre>
<p>多个字符串的合并使用<code>+</code>不是最好的方式，大家想想为什么？ 有什么更好的方式呢？ 参见文章<a href="https://mp.weixin.qq.com/s/n5kkZfC8FGlzeBODarLHcw">为啥我的Python这么慢 (一)</a>。</p>
</div>
<div id="section-1.4" class="section level2">
<h2><span class="header-section-number">1.4</span> 变量</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#变量就像一个盒子一样</span>
a <span class="op">=</span> <span class="dv">5</span>
a</code></pre></div>
<pre><code>5</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">b <span class="op">=</span> <span class="dv">3</span>
b</code></pre></div>
<pre><code>3</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">+</span> b</code></pre></div>
<pre><code>8</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#第一次赋值为变量的初始化，再次赋值则被覆写</span>
a <span class="op">=</span> <span class="dv">30</span>
a</code></pre></div>
<pre><code>30</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">c <span class="op">=</span> <span class="st">&#39;hello&#39;</span>
c</code></pre></div>
<pre><code>&#39;hello&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">c <span class="op">=</span> <span class="st">&#39;goodbye&#39;</span>
c</code></pre></div>
<pre><code>&#39;goodbye&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 变量名命名:为清晰表达，驼峰式，下划线式</span>
LookLikeThis <span class="op">=</span> <span class="dv">1</span>
look_like_this <span class="op">=</span> <span class="st">&#39;a&#39;</span></code></pre></div>
</div>
<div id="helloworld.py" class="section level2">
<h2><span class="header-section-number">1.5</span> 第一小程序HelloWorld.py</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 第一小程序的准备</span>
<span class="co">#注释用#</span>
<span class="co"># print() #打印内容显示在屏幕上</span>
<span class="bu">print</span>(<span class="st">&#39;ehbio&#39;</span>)
a <span class="op">=</span> <span class="dv">5</span>
<span class="bu">print</span>(a)  <span class="co"># 打印变量</span></code></pre></div>
<pre><code>ehbio</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">myName <span class="op">=</span> <span class="bu">input</span>()</code></pre></div>
<pre><code>a</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">myName <span class="op">=</span> <span class="bu">input</span>(<span class="st">&#39;please type your name！</span><span class="ch">\n</span><span class="st">&#39;</span>)</code></pre></div>
<pre><code>please type your name！
ehbio</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#len()查看字符串的个数</span>
<span class="bu">len</span>(<span class="st">&#39;hello&#39;</span>)
<span class="bu">len</span>(<span class="st">&#39;hell o&#39;</span>)</code></pre></div>
<pre><code>6</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">##类型转换的函数</span>
<span class="bu">str</span>()
<span class="bu">int</span>()
<span class="bu">float</span>()</code></pre></div>
<pre><code>0.0</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">str</span>(<span class="dv">8</span>)</code></pre></div>
<pre><code>&#39;8&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">int</span>(<span class="fl">2.5</span>)</code></pre></div>
<pre><code>2</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">float</span>(<span class="dv">2</span>)</code></pre></div>
<pre><code>2.0</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="dv">42</span> <span class="op">==</span> <span class="st">&#39;42&#39;</span> <span class="co">#字符和数字不同</span></code></pre></div>
<pre><code>False</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#特殊的值None,函数部分会再次提及此值</span>
<span class="bu">type</span>(<span class="va">None</span>)</code></pre></div>
<pre><code>NoneType</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="va">None</span> <span class="op">==</span> <span class="dv">42</span></code></pre></div>
<pre><code>False</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#This is my second python pargram！</span>

myName<span class="op">=</span><span class="bu">input</span>(<span class="st">&#39;What is your name?&#39;</span>)
<span class="bu">print</span> (<span class="st">&#39;It is  good to meet you,&#39;</span> <span class="op">+</span> myName)
<span class="bu">print</span> (<span class="st">&#39;The length of your name is &#39;</span><span class="op">+</span> <span class="bu">str</span>(<span class="bu">len</span>(myName)))

<span class="bu">print</span>(<span class="st">&#39;What is your age?&#39;</span>)
myAge<span class="op">=</span><span class="bu">input</span>()
<span class="bu">print</span> (<span class="st">&#39;You will be &#39;</span> <span class="op">+</span> <span class="bu">str</span>( <span class="bu">int</span>( myAge)<span class="op">+</span><span class="dv">1</span> )<span class="op">+</span> <span class="st">&#39; in a year.&#39;</span>)</code></pre></div>
<pre><code>What is your name?wang
It is  good to meet you,wang
The length of your name is 4
What is your age?
2
You will be 3 in a year.</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#如上是在交互模式下的运行，那写好脚本如何运行呢？</span>
<span class="im">import</span> os
os.getcwd()</code></pre></div>
<pre><code>&#39;F:\\mega\\ehbio\\python&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">os.chdir(<span class="st">&#39;F:</span><span class="ch">\\</span><span class="st">mega</span><span class="ch">\\</span><span class="st">ehbio</span><span class="ch">\\</span><span class="st">python&#39;</span>)
os.getcwd()</code></pre></div>
<pre><code>&#39;F:\\mega\\ehbio\\python&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#同时展示IPython模式下运行程序</span>
<span class="op">%</span>run helloworld.py</code></pre></div>
</div>
<div id="section-1.6" class="section level2">
<h2><span class="header-section-number">1.6</span> 逻辑和比较操作</h2>
<div id="section-1.6.1" class="section level3">
<h3><span class="header-section-number">1.6.1</span> 布尔逻辑值</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="va">False</span>
a</code></pre></div>
<pre><code>False</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">b <span class="op">=</span> <span class="va">True</span>
b</code></pre></div>
<pre><code>True</code></pre>
</div>
<div id="section-1.6.2" class="section level3">
<h3><span class="header-section-number">1.6.2</span> 比较操作符</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="dv">42</span> <span class="op">==</span> <span class="dv">21</span></code></pre></div>
<pre><code>False</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="dv">42</span> <span class="op">==</span> <span class="st">&#39;42&#39;</span></code></pre></div>
<pre><code>False</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="dv">2</span> <span class="op">!=</span> <span class="dv">1</span></code></pre></div>
<pre><code>True</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="st">&#39;hello&#39;</span>
a <span class="op">==</span> <span class="st">&#39;hello&#39;</span></code></pre></div>
<pre><code>True</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">==</span> <span class="st">&#39;goodbye&#39;</span></code></pre></div>
<pre><code>False</code></pre>
</div>
<div id="-and-or-not" class="section level3">
<h3><span class="header-section-number">1.6.3</span> 布尔操作符 and or not</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="va">True</span> <span class="op">and</span> <span class="va">True</span></code></pre></div>
<pre><code>True</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="va">True</span> <span class="op">&amp;</span> <span class="va">False</span></code></pre></div>
<pre><code>False</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">not</span> <span class="va">False</span></code></pre></div>
<pre><code>True</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">not</span> <span class="op">not</span> <span class="va">True</span></code></pre></div>
<pre><code>True</code></pre>
</div>
<div id="section-1.6.4" class="section level3">
<h3><span class="header-section-number">1.6.4</span> 布尔操作和比较操作符</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">( <span class="dv">3</span> <span class="op">&gt;</span> <span class="dv">2</span> ) <span class="op">and</span> ( <span class="dv">5</span> <span class="op">&lt;</span> <span class="dv">6</span> )</code></pre></div>
<pre><code>True</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">( <span class="dv">3</span> <span class="op">&lt;</span> <span class="dv">2</span> ) <span class="op">or</span> ( <span class="dv">5</span> <span class="op">&lt;</span> <span class="dv">6</span> )</code></pre></div>
<pre><code>True</code></pre>
</div>
</div>
<div id="section-1.7" class="section level2">
<h2><span class="header-section-number">1.7</span> 控制流</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 条件</span>
name <span class="op">=</span> <span class="bu">input</span>(<span class="st">&#39;Please enter a name</span><span class="ch">\n</span><span class="st">&#39;</span>)
<span class="cf">if</span> name <span class="op">==</span> <span class="st">&#39;ehbio&#39;</span>:
    <span class="bu">print</span>(<span class="st">&#39;hello ehbio&#39;</span>)
<span class="cf">else</span>:
    <span class="bu">print</span>(<span class="st">&#39;You are not ehbio&#39;</span>)</code></pre></div>
<pre><code>Please enter a name
ehbio
hello ehbio</code></pre>
<div id="if-" class="section level3">
<h3><span class="header-section-number">1.7.1</span> if 语句</h3>
<p>if 条件（True or False） ：</p>
<pre><code>代码块1</code></pre>
<p>else ：</p>
<pre><code>代码块2</code></pre>
</div>
<div id="elif-" class="section level3">
<h3><span class="header-section-number">1.7.2</span> elif 否则如果</h3>
<p>elif 条件 ：</p>
<pre><code>代码块</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">age <span class="op">=</span> <span class="bu">int</span>(<span class="bu">input</span>(<span class="st">&#39;please enter your age!</span><span class="ch">\n</span><span class="st">&#39;</span>))  <span class="co"># input()从屏幕中录入的内容，属于字符串类型</span>
<span class="cf">if</span> age <span class="op">&lt;</span> <span class="dv">18</span>:
    <span class="bu">print</span>(<span class="st">&#39;Hi, kids&#39;</span>)
<span class="cf">elif</span> <span class="dv">40</span> <span class="op">&gt;=</span> age <span class="op">&gt;=</span> <span class="dv">12</span>:
    <span class="bu">print</span>(<span class="st">&#39;Hi, young man&#39;</span>)
<span class="cf">elif</span> age <span class="op">&gt;</span> <span class="dv">40</span>:
    <span class="bu">print</span>(<span class="st">&#39;Hi, old man&#39;</span>)</code></pre></div>
<pre><code>please enter your age!
34
Hi, young man</code></pre>
</div>
<div id="while" class="section level3">
<h3><span class="header-section-number">1.7.3</span> while循环</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="dv">0</span>
<span class="cf">if</span> a <span class="op">&lt;</span> <span class="dv">5</span>:
    <span class="bu">print</span>(<span class="st">&#39;Hello, world&#39;</span>)
    a <span class="op">=</span> a <span class="op">+</span> <span class="dv">1</span></code></pre></div>
<pre><code>Hello, world</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="dv">0</span>
<span class="cf">while</span> a <span class="op">&lt;</span> <span class="dv">5</span>:
    <span class="bu">print</span>(<span class="st">&#39;Hello, world&#39;</span>)
    a <span class="op">=</span> a <span class="op">+</span> <span class="dv">1</span></code></pre></div>
<pre><code>Hello, world
Hello, world
Hello, world
Hello, world
Hello, world</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">name <span class="op">=</span> <span class="st">&#39;&#39;</span>
<span class="cf">while</span> name <span class="op">!=</span> <span class="st">&#39;Your name&#39;</span>:
    name <span class="op">=</span> <span class="bu">input</span>(<span class="st">&#39;Enter your name</span><span class="ch">\n</span><span class="st">&#39;</span>)
<span class="bu">print</span>(<span class="st">&#39;Thank you&#39;</span>)</code></pre></div>
<pre><code>Enter your name
your name
Thank you</code></pre>
</div>
<div id="breakcontinue" class="section level3">
<h3><span class="header-section-number">1.7.4</span> break和continue</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">while</span> <span class="va">True</span>:
    name <span class="op">=</span> <span class="bu">input</span>(<span class="st">&#39;Please enter your name!</span><span class="ch">\n</span><span class="st">&#39;</span>)
    <span class="cf">if</span> name <span class="op">==</span> <span class="st">&#39;Your name&#39;</span>:
        <span class="cf">break</span>  <span class="co"># 跳出循环</span>
<span class="bu">print</span>(<span class="st">&#39;You are right!&#39;</span>)</code></pre></div>
<pre><code>Please enter your name!
Your name
You are right!</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 模拟登陆账号</span>
<span class="cf">while</span> <span class="va">True</span>:
    name <span class="op">=</span> <span class="bu">input</span>(<span class="st">&#39;Who are you?</span><span class="ch">\n</span><span class="st">&#39;</span>)
    <span class="cf">if</span> name <span class="op">!=</span> <span class="st">&#39;Bob&#39;</span>:
        <span class="cf">continue</span>  <span class="co"># 将程序跳转到开头</span>
    <span class="bu">print</span>(<span class="st">&#39;Hello, Bob. What is your password?&#39;</span>)
    password <span class="op">=</span> <span class="bu">input</span>()
    <span class="cf">if</span> password <span class="op">==</span> <span class="st">&#39;fish&#39;</span>:
        <span class="cf">break</span>
<span class="bu">print</span>(<span class="st">&#39;Access granted!&#39;</span>)</code></pre></div>
<pre><code>Who are you?
Bob
Hello, Bob. What is your password?
fish
Acess granted!</code></pre>
</div>
<div id="for--range" class="section level3">
<h3><span class="header-section-number">1.7.5</span> for 和 range（）函数</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># for 和 range（）实现固定的循环次数</span>
<span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">5</span>):
    <span class="bu">print</span>(i)
    <span class="bu">print</span>(<span class="st">&#39;Hello world&#39;</span>)</code></pre></div>
<pre><code>0
Hello world
1
Hello world
2
Hello world
3
Hello world
4
Hello world</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 等价的while循环</span>
i <span class="op">=</span> <span class="dv">0</span>
<span class="cf">while</span> i <span class="op">&lt;</span> <span class="dv">5</span>:
    <span class="bu">print</span>(i)
    <span class="bu">print</span>(<span class="st">&#39;Hello world&#39;</span>)
    i <span class="op">=</span> i <span class="op">+</span> <span class="dv">1</span></code></pre></div>
<pre><code>0
Hello world
1
Hello world
2
Hello world
3
Hello world
4
Hello world</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 高斯的1+2+3+...+100=?</span>
total <span class="op">=</span> <span class="dv">0</span>
<span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">101</span>):
    total <span class="op">=</span> total <span class="op">+</span> i
<span class="bu">print</span>(total)</code></pre></div>
<pre><code>5050</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 高斯的1+2+3+...+100=?</span>
total <span class="op">=</span> <span class="dv">0</span>
<span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">101</span>):
    total <span class="op">+=</span> i
<span class="bu">print</span>(total)</code></pre></div>
<pre><code>5050</code></pre>
</div>
<div id="range" class="section level3">
<h3><span class="header-section-number">1.7.6</span> range()函数（开始，停止，步长）</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">1</span>, <span class="dv">10</span>, <span class="dv">2</span>):
    <span class="bu">print</span>(i)</code></pre></div>
<pre><code>1
3
5
7
9</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">help</span>(<span class="bu">range</span>)</code></pre></div>
<pre><code>Help on class range in module builtins:

class range(object)
 |  range(stop) -&gt; range object
 |  range(start, stop[, step]) -&gt; range object
 |  
 |  Return an object that produces a sequence of integers from start (inclusive)
 |  to stop (exclusive) by step.  range(i, j) produces i, i+1, i+2, ..., j-1.
 |  start defaults to 0, and stop is omitted!  range(4) produces 0, 1, 2, 3.
 |  These are exactly the valid indices for a list of 4 elements.
 |  When step is given, it specifies the increment (or decrement).
 |  
 |  Methods defined here:
 |  
 |  __contains__(self, key, /)
 |      Return key in self.
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self&gt;=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getitem__(self, key, /)
 |      Return self[key].
 |  
 |  __gt__(self, value, /)
 |      Return self&gt;value.
 |  
 |  __hash__(self, /)
 |      Return hash(self).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __le__(self, value, /)
 |      Return self&lt;=value.
 |  
 |  __len__(self, /)
 |      Return len(self).
 |  
 |  __lt__(self, value, /)
 |      Return self&lt;value.
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  __reduce__(...)
 |      helper for pickle
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __reversed__(...)
 |      Return a reverse iterator.
 |  
 |  count(...)
 |      rangeobject.count(value) -&gt; integer -- return number of occurrences of value
 |  
 |  index(...)
 |      rangeobject.index(value, [start, [stop]]) -&gt; integer -- return index of value.
 |      Raise ValueError if the value is not present.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  start
 |  
 |  step
 |  
 |  stop</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">5</span>, <span class="op">-</span><span class="dv">1</span>, <span class="op">-</span><span class="dv">1</span>):
    <span class="bu">print</span>(i)</code></pre></div>
<pre><code>5
4
3
2
1
0</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 脑筋急转弯，题目如下</span>
<span class="co"># 100元钱，5元铅笔盒，3元一只笔，0.5元一块橡皮，将100元花完，同时三种物品的个数和为100，请用编程解决</span>

<span class="co"># 假设铅笔盒为x个，笔为y只，橡皮z个</span>
<span class="bu">print</span>(<span class="st">&quot;x y z&quot;</span>)
<span class="cf">for</span> x <span class="op">in</span> <span class="bu">range</span>(<span class="dv">1</span>, <span class="dv">101</span>):
    <span class="cf">for</span> y <span class="op">in</span> <span class="bu">range</span>(<span class="dv">1</span>, <span class="dv">101</span>):
        <span class="cf">for</span> z <span class="op">in</span> <span class="bu">range</span>(<span class="dv">1</span>, <span class="dv">101</span>):
            <span class="cf">if</span> x <span class="op">+</span> y <span class="op">+</span> z <span class="op">==</span> <span class="dv">100</span> <span class="op">and</span> <span class="dv">5</span> <span class="op">*</span> x <span class="op">+</span> <span class="dv">3</span> <span class="op">*</span> y <span class="op">+</span> <span class="fl">0.5</span> <span class="op">*</span> z <span class="op">==</span> <span class="dv">100</span>:
                <span class="bu">print</span>(x, y, z)</code></pre></div>
<pre><code>x y z
5 11 84
10 2 88</code></pre>
</div>
</div>
<div id="section-1.8" class="section level2">
<h2><span class="header-section-number">1.8</span> 导入模块</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> random  <span class="co"># 当多个模块时，用逗号隔开</span>
<span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">5</span>):
    <span class="bu">print</span>(random.randint(<span class="dv">1</span>, <span class="dv">10</span>))</code></pre></div>
<pre><code>2
9
2
4
10</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> random <span class="im">as</span> rd
<span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">5</span>):
    <span class="bu">print</span>(rd.randint(<span class="dv">1</span>, <span class="dv">10</span>))</code></pre></div>
<pre><code>2
8
5
7
7</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">from</span> random <span class="im">import</span> randint
<span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">5</span>):
    <span class="bu">print</span>(randint(<span class="dv">1</span>, <span class="dv">10</span>))</code></pre></div>
<pre><code>6
8
5
6
2</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> sys
<span class="im">import</span> os
<span class="im">import</span> math</code></pre></div>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 综合小例子，控制流的结束，执行完成本身即可结束</span>
<span class="im">import</span> sys
<span class="cf">while</span> <span class="va">True</span>:
    <span class="bu">print</span>(<span class="st">&#39;type exit to exit.&#39;</span>)
    response <span class="op">=</span> <span class="bu">input</span>()
    <span class="cf">if</span> response <span class="op">==</span> <span class="st">&#39;exit&#39;</span>:
        sys.exit()  <span class="co"># 退出Python程序</span>
    <span class="bu">print</span>(<span class="st">&#39;Let us have a short break!&#39;</span>)</code></pre></div>
<pre><code>type exit to exit.
exit



An exception has occurred, use %tb to see the full traceback.


SystemExit



C:\Users\Administrator\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:2870: UserWarning: To exit: use &#39;exit&#39;, &#39;quit&#39;, or Ctrl-D.
  warn(&quot;To exit: use &#39;exit&#39;, &#39;quit&#39;, or Ctrl-D.&quot;, stacklevel=1)</code></pre>
</div>
<div id="-" class="section level2">
<h2><span class="header-section-number">1.9</span> 函数： 内置函数、自定义函数</h2>
<div id="printlen-input-intstr" class="section level3">
<h3><span class="header-section-number">1.9.1</span> 函数print（），len （）,input （），int（），str（）均为内置函数</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">help</span>(<span class="bu">print</span>)</code></pre></div>
<pre><code>Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=&#39; &#39;, end=&#39;\n&#39;, file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span>(<span class="st">&#39;a&#39;</span>, <span class="st">&#39;b&#39;</span>)</code></pre></div>
<pre><code>a b</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 自定义函数</span>
<span class="kw">def</span> <span class="bu">sum</span>(a, b):
    c <span class="op">=</span> a <span class="op">+</span> b
    <span class="bu">print</span>(c)


<span class="bu">sum</span>(<span class="dv">3</span>, <span class="dv">4</span>)</code></pre></div>
<pre><code>7</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> <span class="bu">sum</span>(a, b):  <span class="co"># a,b为自定义函数中的两个参数</span>
    c <span class="op">=</span> a <span class="op">+</span> b
    <span class="cf">return</span> c


<span class="bu">sum</span>(<span class="dv">1</span>, <span class="dv">3</span>)</code></pre></div>
<pre><code>4</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> random


<span class="kw">def</span> getAnswer(answerNumber):
    <span class="cf">if</span> answerNumber <span class="op">==</span> <span class="dv">1</span>:
        <span class="cf">return</span> <span class="st">&#39;It is certain&#39;</span>
    <span class="cf">elif</span> answerNumber <span class="op">==</span> <span class="dv">2</span>:
        <span class="cf">return</span> <span class="st">&#39;It is decidely so&#39;</span>
    <span class="cf">elif</span> answerNumber <span class="op">==</span> <span class="dv">3</span>:
        <span class="cf">return</span> <span class="st">&#39;Yes&#39;</span>
    <span class="cf">elif</span> answerNumber <span class="op">==</span> <span class="dv">4</span>:
        <span class="cf">return</span> <span class="st">&#39;Reply hazy try again&#39;</span>
    <span class="cf">elif</span> answerNumber <span class="op">==</span> <span class="dv">5</span>:
        <span class="cf">return</span> <span class="st">&#39;Ask again later&#39;</span>
    <span class="cf">elif</span> answerNumber <span class="op">==</span> <span class="dv">6</span>:
        <span class="cf">return</span> <span class="st">&#39;Concentrate and ask again&#39;</span>
    <span class="cf">elif</span> answerNumber <span class="op">==</span> <span class="dv">7</span>:
        <span class="cf">return</span> <span class="st">&#39;My reply is no&#39;</span>
    <span class="cf">elif</span> answerNumber <span class="op">==</span> <span class="dv">8</span>:
        <span class="cf">return</span> <span class="st">&#39;Outlook not so good&#39;</span>
    <span class="cf">elif</span> answerNumber <span class="op">==</span> <span class="dv">9</span>:
        <span class="cf">return</span> <span class="st">&#39;Very doubtful&#39;</span>


r <span class="op">=</span> random.randint(<span class="dv">1</span>, <span class="dv">9</span>)
fortune <span class="op">=</span> getAnswer(r)
<span class="bu">print</span>(r, fortune)</code></pre></div>
<pre><code>8 Outlook not so good</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 关于None的值</span>
<span class="bu">print</span>()  <span class="co"># 打印内容为空，所以为None，其他编程语言可能为NA，null等</span></code></pre></div>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span>(<span class="st">&#39;cat&#39;</span>, <span class="st">&#39;dog&#39;</span>, <span class="st">&#39;mice&#39;</span>)  <span class="co"># 位置参数</span></code></pre></div>
<pre><code>cat dog mice</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span>(<span class="st">&#39;cat&#39;</span>, <span class="st">&#39;dog&#39;</span>, <span class="st">&#39;mice&#39;</span>, sep<span class="op">=</span><span class="st">&#39;*&#39;</span>, end<span class="op">=</span><span class="st">&#39;88888&#39;</span>)  <span class="co"># end，sep就是关键字参数</span></code></pre></div>
<pre><code>cat*dog*mice88888</code></pre>
</div>
</div>
<div id="section-1.10" class="section level2">
<h2><span class="header-section-number">1.10</span> 局部和全局作用域</h2>
<p>全局作用：函数之外。</p>
<p>局部作用：函数之内。</p>
<p>局部可以访问全局变量，而全局不能使用局部变量。</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> spam():
    eggs <span class="op">=</span> <span class="dv">1000</span>
    <span class="cf">return</span> eggs


spam()</code></pre></div>
<pre><code>1000</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">eggs <span class="co">#报错：eggs未被定义，因为eggs是自定义函数spam()中的局部变量</span></code></pre></div>
<pre><code>---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

&lt;ipython-input-90-6eaa7316e4ae&gt; in &lt;module&gt;()
----&gt; 1 eggs


NameError: name &#39;eggs&#39; is not defined</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 同理局部变量不能使用其他局部变量</span>
<span class="kw">def</span> ehbio():
    eggs <span class="op">=</span> <span class="dv">99</span>
    spam()
    <span class="bu">print</span>(eggs)


<span class="kw">def</span> spam():
    eggs <span class="op">=</span> <span class="dv">0</span>
    <span class="cf">return</span> eggs


ehbio()</code></pre></div>
<pre><code>99</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">spam()</code></pre></div>
<pre><code>0</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 全局变量在局部作用域中使用</span>
<span class="kw">def</span> spam():
    <span class="bu">print</span>(eggs)


eggs <span class="op">=</span> <span class="dv">28</span>
spam()</code></pre></div>
<pre><code>28</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span>(eggs)</code></pre></div>
<pre><code>28</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 尽量避免名称相同的局部变量和全局变量</span>
<span class="kw">def</span> spam():
    eggs <span class="op">=</span> <span class="st">&#39;spam local&#39;</span>
    <span class="bu">print</span>(eggs)  <span class="co"># 输出spam local</span>


<span class="kw">def</span> bacon():
    eggs <span class="op">=</span> <span class="st">&#39;bacon local&#39;</span>
    <span class="bu">print</span>(eggs)  <span class="co"># 输出bacon local</span>
    spam()
    <span class="bu">print</span>(eggs)  <span class="co"># 输出bacon local</span>


eggs <span class="op">=</span> <span class="st">&#39;global&#39;</span>
bacon()</code></pre></div>
<pre><code>bacon local
spam local
bacon local</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span>(eggs)</code></pre></div>
<pre><code>global</code></pre>
</div>
<div id="global" class="section level2">
<h2><span class="header-section-number">1.11</span> 声明为全局变量global</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 在函数内修改全局变量</span>
<span class="kw">def</span> spam():
    <span class="kw">global</span> eggs  <span class="co"># 声明为全局变量</span>
    eggs <span class="op">=</span> <span class="dv">5</span>


eggs <span class="op">=</span> <span class="dv">7</span>
spam()  <span class="co"># 调用自定义函数spam()</span>
<span class="bu">print</span>(eggs)</code></pre></div>
<pre><code>5</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> spam():
    <span class="kw">global</span> eggs  <span class="co"># this is the global</span>
    eggs <span class="op">=</span> <span class="st">&#39;spam&#39;</span>


<span class="kw">def</span> bacon():
    eggs <span class="op">=</span> <span class="st">&#39;bacon&#39;</span>  <span class="co"># this is a local</span>


<span class="kw">def</span> ham():
    <span class="bu">print</span>(eggs)  <span class="co"># this is the global</span>


eggs <span class="op">=</span> <span class="dv">42</span>  <span class="co"># this is the global</span>
spam()
<span class="bu">print</span>(eggs)</code></pre></div>
<pre><code>spam</code></pre>
</div>
<div id="section-1.12" class="section level2">
<h2><span class="header-section-number">1.12</span> 异常的处理</h2>
<p>自定义函数可能报错，报错的处理</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> spam(divideBy):
    <span class="cf">return</span> <span class="dv">21</span> <span class="op">/</span> divideBy


spam(<span class="dv">3</span>)
spam(<span class="dv">7</span>)</code></pre></div>
<pre><code>3.0</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">spam(<span class="dv">0</span>)  <span class="co"># 如果是0，则会报错</span></code></pre></div>
<pre><code>---------------------------------------------------------------------------

ZeroDivisionError                         Traceback (most recent call last)

&lt;ipython-input-107-b3fca5f67bd4&gt; in &lt;module&gt;()
----&gt; 1 spam(0)


&lt;ipython-input-106-f3d08c8e42a5&gt; in spam(divideBy)
      1 def spam(divideBy):
----&gt; 2     return 21/divideBy
      3 spam(3)
      4 spam(7)


ZeroDivisionError: division by zero</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 此时，我们可以借助try 和 except解决</span>
<span class="kw">def</span> spam(divideBy):
    <span class="cf">try</span>:
        <span class="cf">return</span> <span class="dv">21</span> <span class="op">/</span> divideBy
    <span class="cf">except</span> <span class="pp">ZeroDivisionError</span>:
        <span class="bu">print</span>(<span class="st">&#39;Error: invalid argument&#39;</span>)


spam(<span class="dv">0</span>)
spam(<span class="dv">3</span>)</code></pre></div>
<pre><code>Error: invalid argument





7.0</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 以上为知道报错内容，如果不知道报错内容呢？</span>
<span class="kw">def</span> spam(divideBy):
    <span class="cf">try</span>:
        <span class="cf">return</span> <span class="dv">21</span> <span class="op">/</span> divideBy
    <span class="cf">except</span>:
        <span class="bu">print</span>(<span class="st">&#39;Error: invalid argument&#39;</span>)


spam(<span class="dv">0</span>)</code></pre></div>
<pre><code>Error: invalid argument</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> traceback


<span class="kw">def</span> spam(divideBy):
    <span class="cf">try</span>:
        <span class="cf">return</span> <span class="dv">21</span> <span class="op">/</span> divideBy
    <span class="cf">except</span>:
        traceback.print_exc()


spam(<span class="dv">0</span>)</code></pre></div>
<pre><code>Traceback (most recent call last):
  File &quot;&lt;ipython-input-115-9238a8901fff&gt;&quot;, line 4, in spam
    return 21/ divideBy
ZeroDivisionError: division by zero</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 用sys模块</span>
<span class="im">import</span> sys


<span class="kw">def</span> spam(divideBy):
    <span class="cf">try</span>:
        <span class="cf">return</span> <span class="dv">21</span> <span class="op">/</span> divideBy
    <span class="cf">except</span>:
        info <span class="op">=</span> sys.exc_info()
        <span class="co">#print (info)</span>
        <span class="bu">print</span>(info[<span class="dv">0</span>], <span class="st">&quot;:&quot;</span>, info[<span class="dv">1</span>])


spam(<span class="dv">0</span>)</code></pre></div>
<pre><code>&lt;class &#39;ZeroDivisionError&#39;&gt; : division by zero</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 只有6次机会的猜字游戏</span>
<span class="co"># This is a guess the number game.</span>
<span class="im">import</span> random
secretNumber <span class="op">=</span> random.randint(<span class="dv">1</span>, <span class="dv">20</span>)
<span class="bu">print</span>(<span class="st">&#39;I am thinking of a number between 1 and 20.&#39;</span>)

<span class="co"># Ask the player to guess 6 times.</span>
<span class="cf">for</span> guessesTaken <span class="op">in</span> <span class="bu">range</span>(<span class="dv">1</span>, <span class="dv">7</span>):
    <span class="bu">print</span>(<span class="st">&#39;Take a guess.&#39;</span>)
    guess <span class="op">=</span> <span class="bu">int</span>(<span class="bu">input</span>())

    <span class="cf">if</span> guess <span class="op">&lt;</span> secretNumber:
        <span class="bu">print</span>(<span class="st">&#39;Your guess is too low.&#39;</span>)
    <span class="cf">elif</span> guess <span class="op">&gt;</span> secretNumber:
        <span class="bu">print</span>(<span class="st">&#39;Your guess is too high.&#39;</span>)
    <span class="cf">else</span>:
        <span class="cf">break</span>  <span class="co"># This condition is the correct guess!</span>

<span class="cf">if</span> guess <span class="op">==</span> secretNumber:
    <span class="bu">print</span>(<span class="st">&#39;Good job! You guessed my number in &#39;</span> <span class="op">+</span>
          <span class="bu">str</span>(guessesTaken) <span class="op">+</span> <span class="st">&#39; guesses!&#39;</span>)
<span class="cf">else</span>:
    <span class="bu">print</span>(<span class="st">&#39;Nope. The number I was thinking of was &#39;</span> <span class="op">+</span> <span class="bu">str</span>(secretNumber))</code></pre></div>
<pre><code>I am thinking of a number between 1 and 20.
Take a guess.
1
Your guess is too low.
Take a guess.
6
Your guess is too low.
Take a guess.
5
Your guess is too low.
Take a guess.
3
Your guess is too low.
Take a guess.
4
Your guess is too low.
Take a guess.
6
Your guess is too low.
Nope. The number I was thinking of was 7</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># collatz序列，如果是偶数则打印n//2， 如果是奇数则打印3*n+1</span>
<span class="kw">def</span> collatz(n):
    <span class="bu">print</span>(n)
    <span class="cf">if</span> n <span class="op">%</span> <span class="dv">2</span> <span class="op">==</span> <span class="dv">1</span> <span class="op">and</span> n <span class="op">&gt;</span> <span class="dv">1</span>:
        collatz(<span class="dv">3</span> <span class="op">*</span> n <span class="op">+</span> <span class="dv">1</span>)
    <span class="cf">elif</span> n <span class="op">%</span> <span class="dv">2</span> <span class="op">==</span> <span class="dv">0</span>:
        collatz(n <span class="op">//</span> <span class="dv">2</span>)


n <span class="op">=</span> <span class="bu">input</span>(<span class="st">&#39;enter a number:&#39;</span>)
n <span class="op">=</span> <span class="bu">int</span>(n)
collatz(n)</code></pre></div>
<pre><code>enter a number:4
4
2
1</code></pre>
</div>
<div id="section-1.13" class="section level2">
<h2><span class="header-section-number">1.13</span> 列表</h2>
<p>“列表”是一个值，比如[1,3,3,‘cat’]</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">type</span>([<span class="dv">1</span>, <span class="dv">2</span>, <span class="st">&#39;a&#39;</span>])</code></pre></div>
<pre><code>list</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="st">&#39;a&#39;</span>, <span class="st">&#39;b&#39;</span>]
a</code></pre></div>
<pre><code>[1, 2, 3, 4, &#39;a&#39;, &#39;b&#39;]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">b <span class="op">=</span> [a, <span class="dv">3</span>, <span class="dv">4</span>]  <span class="co"># 列表可以嵌套列表，字典等</span>
b</code></pre></div>
<pre><code>[[1, 2, 3, 4, &#39;a&#39;, &#39;b&#39;], 3, 4]</code></pre>
<p>取列表中单个值</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> [i <span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">5</span>)]
a</code></pre></div>
<pre><code>[0, 1, 2, 3, 4]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a[<span class="dv">0</span>]  <span class="co"># 用元素的下标取值</span></code></pre></div>
<pre><code>0</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a[<span class="dv">3</span>]</code></pre></div>
<pre><code>3</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">zoo <span class="op">=</span> [<span class="st">&#39;cat&#39;</span>, <span class="st">&#39;dog&#39;</span>, <span class="st">&#39;rat&#39;</span>, <span class="st">&#39;elephant&#39;</span>]
zoo[<span class="dv">3</span>]</code></pre></div>
<pre><code>&#39;elephant&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">zoo[<span class="dv">0</span>:<span class="dv">2</span>]</code></pre></div>
<pre><code>[&#39;cat&#39;, &#39;dog&#39;]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;hello &#39;</span> <span class="op">+</span> zoo[<span class="dv">3</span>]</code></pre></div>
<pre><code>&#39;hello elephant&#39;</code></pre>
<p>负数下标</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">zoo[<span class="op">-</span><span class="dv">1</span>]</code></pre></div>
<pre><code>&#39;elephant&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> [<span class="st">&#39;b&#39;</span>, <span class="st">&#39;c&#39;</span>, <span class="st">&#39;d&#39;</span>]
zoo <span class="op">=</span> [<span class="st">&#39;cat&#39;</span>, <span class="st">&#39;dog&#39;</span>, <span class="st">&#39;rat&#39;</span>, <span class="st">&#39;elephant&#39;</span>]
total <span class="op">=</span> [a, zoo]</code></pre></div>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">total</code></pre></div>
<pre><code>[[&#39;b&#39;, &#39;c&#39;, &#39;d&#39;], [&#39;cat&#39;, &#39;dog&#39;, &#39;rat&#39;, &#39;elephant&#39;]]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">total[<span class="dv">0</span>][<span class="dv">1</span>]</code></pre></div>
<pre><code>&#39;c&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">total[<span class="dv">1</span>][<span class="dv">0</span>]</code></pre></div>
<pre><code>&#39;cat&#39;</code></pre>
<p>zoo[0] 列表和下标 zoo[0:2]列表和切片</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">zoo[<span class="dv">0</span>:<span class="dv">2</span>]</code></pre></div>
<pre><code>[&#39;cat&#39;, &#39;dog&#39;]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">zoo[<span class="dv">100</span>] <span class="co">#超出后报错</span></code></pre></div>
<pre><code>---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

&lt;ipython-input-13-034b9400bdc5&gt; in &lt;module&gt;()
----&gt; 1 zoo[100]


IndexError: list index out of range</code></pre>
<p>查看列表长度</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">len</span>(zoo)</code></pre></div>
<pre><code>4</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 用下标更改列表的值</span>
zoo <span class="op">=</span> [<span class="st">&#39;cat&#39;</span>, <span class="st">&#39;dog&#39;</span>, <span class="st">&#39;rat&#39;</span>, <span class="st">&#39;elephant&#39;</span>]
zoo</code></pre></div>
<pre><code>[&#39;cat&#39;, &#39;dog&#39;, &#39;rat&#39;, &#39;elephant&#39;]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">zoo[<span class="dv">0</span>] <span class="op">=</span> <span class="st">&#39;five&#39;</span></code></pre></div>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">zoo</code></pre></div>
<pre><code>[&#39;five&#39;, &#39;dog&#39;, &#39;rat&#39;, &#39;elephant&#39;]</code></pre>
<p>列表的连接和复制</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> [i <span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">5</span>)]
a</code></pre></div>
<pre><code>[0, 1, 2, 3, 4]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">b <span class="op">=</span> [i <span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">3</span>, <span class="dv">10</span>, <span class="dv">2</span>)]
b</code></pre></div>
<pre><code>[3, 5, 7, 9]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">+</span> b</code></pre></div>
<pre><code>[0, 1, 2, 3, 4, 3, 5, 7, 9]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">*</span> <span class="dv">3</span>  <span class="co"># 列表的复制</span></code></pre></div>
<pre><code>[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4]</code></pre>
<p>删除某个元素</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">del</span> a[<span class="dv">0</span>] <span class="co">#删除运行中的某个变量</span></code></pre></div>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a</code></pre></div>
<pre><code>[1, 2, 3, 4]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">del</span> a[<span class="dv">2</span>]</code></pre></div>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a</code></pre></div>
<pre><code>[1, 2, 4]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 应用列表的实例</span>
bookNames <span class="op">=</span> []
<span class="cf">while</span> <span class="va">True</span>:
    <span class="bu">print</span>(<span class="st">&#39;Enter the name of books&#39;</span> <span class="op">+</span>
          <span class="bu">str</span>(<span class="bu">len</span>(bookNames) <span class="op">+</span> <span class="dv">1</span>) <span class="op">+</span> <span class="st">&#39;Or enter nothing to stop&#39;</span>)
    name <span class="op">=</span> <span class="bu">input</span>()
    <span class="cf">if</span> name <span class="op">==</span> <span class="st">&#39;&#39;</span>:
        <span class="cf">break</span>
    bookNames <span class="op">=</span> bookNames <span class="op">+</span> [name]
<span class="bu">print</span>(<span class="st">&#39;The books are:</span><span class="ch">\n</span><span class="st">&#39;</span>)
<span class="cf">for</span> name <span class="op">in</span> bookNames:
    <span class="bu">print</span>(<span class="st">&#39; &#39;</span> <span class="op">+</span> name)</code></pre></div>
<pre><code>enter the name of books1Or enter nothing to stop
lives
enter the name of books2Or enter nothing to stop
python
enter the name of books3Or enter nothing to stop
linux
enter the name of books4Or enter nothing to stop

The books are:

 lives
 python
 linux</code></pre>
<p>列表在循环中的应用</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">5</span>):
    <span class="bu">print</span>(i)</code></pre></div>
<pre><code>0
1
2
3
4</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">for</span> i <span class="op">in</span> [<span class="dv">0</span>, <span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>]:
    <span class="bu">print</span>(i)</code></pre></div>
<pre><code>0
1
2
3
4</code></pre>
<p>in 和 not in操作</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">tool <span class="op">=</span> [<span class="st">&#39;python&#39;</span>, <span class="st">&#39;R&#39;</span>, <span class="st">&#39;perl&#39;</span>, <span class="st">&#39;java&#39;</span>]
<span class="co">&#39;python&#39;</span> <span class="op">in</span> tool</code></pre></div>
<pre><code>True</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;C&#39;</span> <span class="op">not</span> <span class="op">in</span> tool</code></pre></div>
<pre><code>True</code></pre>
<p>应用列表多重赋值</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">tool <span class="op">=</span> [<span class="st">&#39;python&#39;</span>, <span class="st">&#39;R&#39;</span>, <span class="st">&#39;perl&#39;</span>, <span class="st">&#39;java&#39;</span>]
first <span class="op">=</span> tool[<span class="dv">0</span>]
second <span class="op">=</span> tool[<span class="dv">1</span>]
third <span class="op">=</span> tool[<span class="dv">2</span>]
forth <span class="op">=</span> tool[<span class="dv">3</span>]
<span class="bu">print</span>(first, second, third, forth)</code></pre></div>
<pre><code>python R perl java</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 另一种方式</span>
tool <span class="op">=</span> [<span class="st">&#39;python&#39;</span>, <span class="st">&#39;R&#39;</span>, <span class="st">&#39;perl&#39;</span>, <span class="st">&#39;java&#39;</span>]
first, second, third, forth <span class="op">=</span> tool
<span class="bu">print</span>(first, second, third, forth)</code></pre></div>
<pre><code>python R perl java</code></pre>
<p>增强的赋值操作</p>
<pre><code>### +=     -=    *=     %=  注意：+= *=可以完成字符串连接和复制</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="dv">32</span>
a <span class="op">+=</span> <span class="dv">1</span>  <span class="co"># 等价于a = a + 1</span>
a</code></pre></div>
<pre><code>33</code></pre>
<p>方法和函数是一回事，每个数据类型都有一些自己的一组方法。列表数据：查找、添加、删除或操作列表值</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">tool <span class="op">=</span> [<span class="st">&#39;python&#39;</span>, <span class="st">&#39;R&#39;</span>, <span class="st">&#39;perl&#39;</span>, <span class="st">&#39;java&#39;</span>]
tool.index(<span class="st">&#39;R&#39;</span>)</code></pre></div>
<pre><code>1</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">tool.index(<span class="st">&#39;perl&#39;</span>)</code></pre></div>
<pre><code>2</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">tool.index(<span class="st">&#39;C&#39;</span>)  <span class="co"># 没有在list中的情况</span></code></pre></div>
<pre><code>---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

&lt;ipython-input-44-385ebd06ddfe&gt; in &lt;module&gt;()
----&gt; 1 tool.index(&#39;C&#39;)


ValueError: &#39;C&#39; is not in list</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 添加</span>
tool <span class="op">=</span> [<span class="st">&#39;python&#39;</span>, <span class="st">&#39;R&#39;</span>, <span class="st">&#39;perl&#39;</span>, <span class="st">&#39;java&#39;</span>]
tool.append(<span class="st">&#39;C&#39;</span>)  <span class="co"># 没执行一次就多一个</span>
tool</code></pre></div>
<pre><code>[&#39;python&#39;, &#39;R&#39;, &#39;perl&#39;, &#39;java&#39;, &#39;C&#39;]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 插入</span>
tool <span class="op">=</span> [<span class="st">&#39;python&#39;</span>, <span class="st">&#39;R&#39;</span>, <span class="st">&#39;perl&#39;</span>, <span class="st">&#39;java&#39;</span>]
tool.insert(<span class="dv">1</span>, <span class="st">&#39;C&#39;</span>)
tool</code></pre></div>
<pre><code>[&#39;python&#39;, &#39;C&#39;, &#39;R&#39;, &#39;perl&#39;, &#39;java&#39;]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 插入和添加，只能在列表上用 &#39;str&#39; object has no attribute &#39;append&#39;</span>
eggs <span class="op">=</span> <span class="st">&#39;hello&#39;</span>
eggs.append(<span class="st">&#39;o&#39;</span>)</code></pre></div>
<pre><code>---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

&lt;ipython-input-51-f76c80edd988&gt; in &lt;module&gt;()
      1 #插入和添加，只能在列表上用
      2 eggs=&#39;hello&#39;
----&gt; 3 eggs.append(&#39;o&#39;)


AttributeError: &#39;str&#39; object has no attribute &#39;append&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 插入和添加，只能在列表上用 &#39;str&#39; object has no attribute &#39;insert&#39;</span>
eggs <span class="op">=</span> <span class="st">&#39;hello&#39;</span>
eggs.insert(<span class="dv">1</span>, <span class="st">&#39;o&#39;</span>)</code></pre></div>
<pre><code>---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

&lt;ipython-input-52-21aceb5fda09&gt; in &lt;module&gt;()
      1 #插入和添加，只能在列表上用 &#39;str&#39; object has no attribute &#39;append&#39;
      2 eggs=&#39;hello&#39;
----&gt; 3 eggs.insert(1,&#39;o&#39;)


AttributeError: &#39;str&#39; object has no attribute &#39;insert&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 删除</span>
tool <span class="op">=</span> [<span class="st">&#39;python&#39;</span>, <span class="st">&#39;R&#39;</span>, <span class="st">&#39;perl&#39;</span>, <span class="st">&#39;java&#39;</span>]
tool.remove(<span class="st">&#39;java&#39;</span>)
tool</code></pre></div>
<pre><code>[&#39;python&#39;, &#39;R&#39;, &#39;perl&#39;]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 删除不存在的值将报错</span>
tool <span class="op">=</span> [<span class="st">&#39;python&#39;</span>, <span class="st">&#39;R&#39;</span>, <span class="st">&#39;perl&#39;</span>, <span class="st">&#39;java&#39;</span>]
tool.remove(<span class="st">&quot;C&quot;</span>)</code></pre></div>
<pre><code>---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

&lt;ipython-input-54-c1e62b8d0701&gt; in &lt;module&gt;()
      1 #删除不存在的值将报错
      2 tool = [&#39;python&#39;, &#39;R&#39;, &#39;perl&#39;, &#39;java&#39; ]
----&gt; 3 tool.remove(&quot;C&quot;)


ValueError: list.remove(x): x not in list</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 对列表的值进行排序</span>
lt <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">32</span>, <span class="dv">4</span>, <span class="dv">7</span>, <span class="dv">5</span>]
lt.sort()
lt</code></pre></div>
<pre><code>[1, 4, 5, 7, 32]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">lt.sort(reverse<span class="op">=</span><span class="va">True</span>)
lt</code></pre></div>
<pre><code>[32, 7, 5, 4, 1]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">lt <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">9</span>, <span class="dv">2</span>, <span class="st">&#39;a&#39;</span>]  <span class="co"># 不同类型不可排序,sort()使用ASCII码进行排序，因此小写a在Z之前</span>
lt.sort()</code></pre></div>
<pre><code>---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

&lt;ipython-input-89-69cf80e7b764&gt; in &lt;module&gt;()
      1 lt=[1,9,2,&#39;a&#39;]#不同类型不可排序,sort()使用ASCII码进行排序，因此小写a在Z之前
----&gt; 2 lt.sort()


TypeError: &#39;&lt;&#39; not supported between instances of &#39;str&#39; and &#39;int&#39;</code></pre>
</div>
<div id="section-1.14" class="section level2">
<h2><span class="header-section-number">1.14</span> 字符串和元组</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">name <span class="op">=</span> <span class="st">&#39;python&#39;</span>
name[<span class="dv">0</span>:<span class="dv">2</span>]</code></pre></div>
<pre><code>&#39;py&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">name[<span class="op">-</span><span class="dv">1</span>]</code></pre></div>
<pre><code>&#39;n&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;p&#39;</span> <span class="op">in</span> name</code></pre></div>
<pre><code>True</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;p&#39;</span> <span class="op">not</span> <span class="op">in</span> name</code></pre></div>
<pre><code>False</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">for</span> i <span class="op">in</span> name:
    <span class="bu">print</span> (i)</code></pre></div>
<pre><code>p
y
t
h
o
n</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 元组tuple,不可修改</span>
eggs <span class="op">=</span> (<span class="dv">1</span>, <span class="dv">2</span>, <span class="st">&#39;a&#39;</span>)
<span class="bu">type</span>(eggs)</code></pre></div>
<pre><code>tuple</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 不可修改</span>
eggs[<span class="dv">1</span>] <span class="op">=</span> <span class="st">&#39;python&#39;</span></code></pre></div>
<pre><code>---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

&lt;ipython-input-71-d14153220c16&gt; in &lt;module&gt;()
----&gt; 1 eggs[1]=&#39;python&#39;


TypeError: &#39;tuple&#39; object does not support item assignment</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">eggs[<span class="dv">1</span>]</code></pre></div>
<pre><code>2</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#元组用（），列表[]，注意区分如下情况</span>
<span class="bu">type</span>((<span class="st">&#39;hello&#39;</span>))</code></pre></div>
<pre><code>str</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">type</span>((<span class="st">&#39;hello&#39;</span>,)) <span class="co">#逗号在元组中其“重要作用”</span></code></pre></div>
<pre><code>tuple</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">tool <span class="op">=</span> [<span class="st">&#39;python&#39;</span>, <span class="st">&#39;R&#39;</span>, <span class="st">&#39;perl&#39;</span>, <span class="st">&#39;java&#39;</span>]
<span class="bu">tuple</span>(tool)</code></pre></div>
<pre><code>(&#39;python&#39;, &#39;R&#39;, &#39;perl&#39;, &#39;java&#39;)</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> (<span class="dv">1</span>, <span class="dv">3</span>, <span class="dv">4</span>)
<span class="bu">list</span>(a)</code></pre></div>
<pre><code>[1, 3, 4]</code></pre>
</div>
<div id="section-1.15" class="section level2">
<h2><span class="header-section-number">1.15</span> 引用</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="dv">42</span>
b <span class="op">=</span> a
a <span class="op">=</span> <span class="dv">100</span>
a</code></pre></div>
<pre><code>100</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">b</code></pre></div>
<pre><code>42</code></pre>
<p>列表的引用</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> [i <span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">5</span>)]
a</code></pre></div>
<pre><code>[0, 1, 2, 3, 4]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">b <span class="op">=</span> a
b</code></pre></div>
<pre><code>[0, 1, 2, 3, 4]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a[<span class="dv">0</span>] <span class="op">=</span> <span class="st">&#39;hello&#39;</span>
a</code></pre></div>
<pre><code>[&#39;hello&#39;, 1, 2, 3, 4]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">b</code></pre></div>
<pre><code>[&#39;hello&#39;, 1, 2, 3, 4]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> copy  <span class="co"># 列表的复制</span>
a <span class="op">=</span> [<span class="st">&#39;b&#39;</span>, <span class="st">&#39;c&#39;</span>, <span class="st">&#39;d&#39;</span>, <span class="st">&#39;e&#39;</span>]
a</code></pre></div>
<pre><code>[&#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">f <span class="op">=</span> copy.copy(a)
f</code></pre></div>
<pre><code>[&#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">f[<span class="dv">0</span>] <span class="op">=</span> <span class="st">&#39;python&#39;</span></code></pre></div>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">f</code></pre></div>
<pre><code>[&#39;python&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a</code></pre></div>
<pre><code>[&#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;]</code></pre>
<p>deep.copy()如果列表中包含了列表，使用deepcopy()</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">4</span>, <span class="dv">6</span>, [<span class="dv">7</span>, <span class="dv">9</span>]]
b <span class="op">=</span> copy.copy(a)
b</code></pre></div>
<pre><code>[1, 2, 4, 6, [7, 9]]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a[<span class="op">-</span><span class="dv">1</span>][<span class="dv">0</span>] <span class="op">=</span> <span class="dv">70</span>
b</code></pre></div>
<pre><code>[1, 2, 4, 6, [70, 9]]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">4</span>, <span class="dv">6</span>, [<span class="dv">7</span>, <span class="dv">9</span>]]
b <span class="op">=</span> copy.deepcopy(a)
b</code></pre></div>
<pre><code>[1, 2, 4, 6, [7, 9]]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a[<span class="op">-</span><span class="dv">1</span>][<span class="dv">0</span>] <span class="op">=</span> <span class="dv">111</span>
a</code></pre></div>
<pre><code>[1, 2, 4, 6, [111, 9]]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">b</code></pre></div>
<pre><code>[1, 2, 4, 6, [7, 9]]</code></pre>
</div>
<div id="--" class="section level2">
<h2><span class="header-section-number">1.16</span> 字典 键：值 对</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">myTool <span class="op">=</span> {<span class="st">&#39;first&#39;</span>: <span class="st">&#39;python&#39;</span>, <span class="st">&#39;second&#39;</span>: <span class="st">&#39;R&#39;</span>, <span class="st">&#39;third&#39;</span>: <span class="st">&#39;linux&#39;</span>, <span class="st">&#39;forth&#39;</span>: <span class="st">&#39;perl&#39;</span>}
myTool</code></pre></div>
<pre><code>{&#39;first&#39;: &#39;python&#39;, &#39;forth&#39;: &#39;perl&#39;, &#39;second&#39;: &#39;R&#39;, &#39;third&#39;: &#39;linux&#39;}</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">myTool[<span class="st">&#39;first&#39;</span>]</code></pre></div>
<pre><code>&#39;python&#39;</code></pre>
</div>
<div id="section-1.17" class="section level2">
<h2><span class="header-section-number">1.17</span> 字典与列表</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 默认字典是不排序的,不能应用下标或者切片取出，而列表是有序列的</span>
a <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>]
b <span class="op">=</span> [<span class="dv">2</span>, <span class="dv">1</span>, <span class="dv">3</span>]
a <span class="op">==</span> b</code></pre></div>
<pre><code>False</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">myTool1 <span class="op">=</span> {<span class="st">&#39;first&#39;</span>: <span class="st">&#39;python&#39;</span>, <span class="st">&#39;second&#39;</span>: <span class="st">&#39;R&#39;</span>, <span class="st">&#39;third&#39;</span>: <span class="st">&#39;linux&#39;</span>, <span class="st">&#39;forth&#39;</span>: <span class="st">&#39;perl&#39;</span>}
myTool2 <span class="op">=</span> {<span class="st">&#39;first&#39;</span>: <span class="st">&#39;python&#39;</span>, <span class="st">&#39;third&#39;</span>: <span class="st">&#39;linux&#39;</span>, <span class="st">&#39;second&#39;</span>: <span class="st">&#39;R&#39;</span>, <span class="st">&#39;forth&#39;</span>: <span class="st">&#39;perl&#39;</span>}
myTool1 <span class="op">==</span> myTool2</code></pre></div>
<pre><code>True</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#取值通过键</span>
myTool[<span class="st">&#39;second&#39;</span>]</code></pre></div>
<pre><code>---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

&lt;ipython-input-27-ac22a2f1a4f0&gt; in &lt;module&gt;()
      1 #取值通过键
----&gt; 2 myTool[&#39;second&#39;]


NameError: name &#39;myTool&#39; is not defined</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 综合小程序</span>
birthdays <span class="op">=</span> {<span class="st">&#39;Alice&#39;</span>: <span class="st">&#39;Apr 1&#39;</span>, <span class="st">&#39;Bob&#39;</span>: <span class="st">&#39;Dec 12&#39;</span>, <span class="st">&#39;Carol&#39;</span>: <span class="st">&#39;Mar 4&#39;</span>}

<span class="cf">while</span> <span class="va">True</span>:
    <span class="bu">print</span>(<span class="st">&#39;Enter a name: (blank to quit)&#39;</span>)
    name <span class="op">=</span> <span class="bu">input</span>()
    <span class="cf">if</span> name <span class="op">==</span> <span class="st">&#39;&#39;</span>:
        <span class="cf">break</span>

    <span class="cf">if</span> name <span class="op">in</span> birthdays:
        <span class="bu">print</span>(birthdays[name] <span class="op">+</span> <span class="st">&#39; is the birthday of &#39;</span> <span class="op">+</span> name)
    <span class="cf">else</span>:
        <span class="bu">print</span>(<span class="st">&#39;I do not have birthday information for &#39;</span> <span class="op">+</span> name)
        <span class="bu">print</span>(<span class="st">&#39;What is their birthday?&#39;</span>)
        bday <span class="op">=</span> <span class="bu">input</span>()
        birthdays[name] <span class="op">=</span> bday
        <span class="bu">print</span>(<span class="st">&#39;Birthday database updated.&#39;</span>)</code></pre></div>
<pre><code>Enter a name: (blank to quit)
Alice
Apr 1 is the birthday of Alice
Enter a name: (blank to quit)
wang
I do not have birthday information for wang
What is their birthday?
Oct 1
Birthday database updated.
Enter a name: (blank to quit)
wang
Oct 1 is the birthday of wang
Enter a name: (blank to quit)</code></pre>
<div id="keysvaluesitems" class="section level3">
<h3><span class="header-section-number">1.17.1</span> keys()、values（）和items( )</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">myTool <span class="op">=</span> {<span class="st">&#39;first&#39;</span>: <span class="st">&#39;python&#39;</span>, <span class="st">&#39;second&#39;</span>: <span class="st">&#39;R&#39;</span>, <span class="st">&#39;third&#39;</span>: <span class="st">&#39;linux&#39;</span>, <span class="st">&#39;forth&#39;</span>: <span class="st">&#39;perl&#39;</span>}
<span class="cf">for</span> k <span class="op">in</span> myTool.keys():
    <span class="bu">print</span>(k)</code></pre></div>
<pre><code>first
second
third
forth</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">for</span> v <span class="op">in</span> myTool.values():
    <span class="bu">print</span>(v)</code></pre></div>
<pre><code>python
R
linux
perl</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">myTool.items()</code></pre></div>
<pre><code>dict_items([(&#39;first&#39;, &#39;python&#39;), (&#39;second&#39;, &#39;R&#39;), (&#39;third&#39;, &#39;linux&#39;), (&#39;forth&#39;, &#39;perl&#39;)])</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">for</span> k, v <span class="op">in</span> myTool.items():
    <span class="bu">print</span>(<span class="st">&#39;Key: &#39;</span> <span class="op">+</span> k <span class="op">+</span> <span class="st">&#39; Value: &#39;</span> <span class="op">+</span> v)</code></pre></div>
<pre><code>Key: first Value: python
Key: second Value: R
Key: third Value: linux
Key: forth Value: perl</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#检查字典中是否存在键</span>
<span class="co">&#39;first&#39;</span> <span class="op">in</span>  myTool</code></pre></div>
<pre><code>True</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;hello&#39;</span> <span class="op">in</span> myTool</code></pre></div>
<pre><code>False</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># get()，两个参数分别为“键”，如果没有该键，返回的值</span>
myTool <span class="op">=</span> {<span class="st">&#39;first&#39;</span>: <span class="st">&#39;python&#39;</span>, <span class="st">&#39;second&#39;</span>: <span class="st">&#39;R&#39;</span>, <span class="st">&#39;third&#39;</span>: <span class="st">&#39;linux&#39;</span>, <span class="st">&#39;forth&#39;</span>: <span class="st">&#39;perl&#39;</span>}
myTool.get(<span class="st">&#39;first&#39;</span>, <span class="dv">0</span>)</code></pre></div>
<pre><code>&#39;python&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">myTool.get(<span class="st">&#39;hello&#39;</span>, <span class="dv">0</span>)</code></pre></div>
<pre><code>0</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#不使用get()时,如果该键不存在，则报错</span>
myTool[<span class="st">&#39;hello&#39;</span>]</code></pre></div>
<pre><code>---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

&lt;ipython-input-14-72a3aef6d23a&gt; in &lt;module&gt;()
      1 #不使用get()时
----&gt; 2 myTool[&#39;hello&#39;]


KeyError: &#39;hello&#39;</code></pre>
<p>setdefault()如果没有该键，则添加</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 写循环实现</span>
myTool <span class="op">=</span> {<span class="st">&#39;first&#39;</span>: <span class="st">&#39;python&#39;</span>, <span class="st">&#39;second&#39;</span>: <span class="st">&#39;R&#39;</span>, <span class="st">&#39;third&#39;</span>: <span class="st">&#39;linux&#39;</span>, <span class="st">&#39;forth&#39;</span>: <span class="st">&#39;perl&#39;</span>}
<span class="cf">if</span> <span class="st">&#39;fifth&#39;</span> <span class="op">not</span> <span class="op">in</span> myTool:
    myTool[<span class="st">&#39;fifth&#39;</span>] <span class="op">=</span> <span class="st">&#39;C&#39;</span>
myTool</code></pre></div>
<pre><code>{&#39;fifth&#39;: &#39;C&#39;,
 &#39;first&#39;: &#39;python&#39;,
 &#39;forth&#39;: &#39;perl&#39;,
 &#39;second&#39;: &#39;R&#39;,
 &#39;third&#39;: &#39;linux&#39;}</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># setdefault()</span>
myTool <span class="op">=</span> {<span class="st">&#39;first&#39;</span>: <span class="st">&#39;python&#39;</span>, <span class="st">&#39;second&#39;</span>: <span class="st">&#39;R&#39;</span>, <span class="st">&#39;third&#39;</span>: <span class="st">&#39;linux&#39;</span>, <span class="st">&#39;forth&#39;</span>: <span class="st">&#39;perl&#39;</span>}
myTool.setdefault(<span class="st">&#39;fifth&#39;</span>, <span class="st">&#39;C&#39;</span>)</code></pre></div>
<pre><code>&#39;C&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">myTool</code></pre></div>
<pre><code>{&#39;fifth&#39;: &#39;C&#39;,
 &#39;first&#39;: &#39;python&#39;,
 &#39;forth&#39;: &#39;perl&#39;,
 &#39;second&#39;: &#39;R&#39;,
 &#39;third&#39;: &#39;linux&#39;}</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">sequece <span class="op">=</span> <span class="st">&#39;life is short, I use python&#39;</span>
count <span class="op">=</span> {}

<span class="cf">for</span> character <span class="op">in</span> sequece:
    count.setdefault(character, <span class="dv">0</span>)
    count[character] <span class="op">=</span> count[character] <span class="op">+</span> <span class="dv">1</span>

<span class="bu">print</span>(count)</code></pre></div>
<pre><code>{&#39;l&#39;: 1, &#39;i&#39;: 3, &#39;f&#39;: 1, &#39;e&#39;: 2, &#39; &#39;: 5, &#39;s&#39;: 3, &#39;h&#39;: 2, &#39;o&#39;: 2, &#39;r&#39;: 1, &#39;t&#39;: 2, &#39;,&#39;: 1, &#39;u&#39;: 1, &#39;p&#39;: 1, &#39;y&#39;: 1, &#39;n&#39;: 1}</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">sequece <span class="op">=</span> <span class="st">&#39;life is short, I use python&#39;</span>
count <span class="op">=</span> {}

<span class="cf">for</span> character <span class="op">in</span> sequece:
    count[character] <span class="op">=</span> count.get(character,<span class="dv">0</span>) <span class="op">+</span> <span class="dv">1</span>

<span class="bu">print</span>(count)</code></pre></div>
<pre><code>{&#39;l&#39;: 1, &#39;i&#39;: 2, &#39;f&#39;: 1, &#39;e&#39;: 2, &#39; &#39;: 5, &#39;s&#39;: 3, &#39;h&#39;: 2, &#39;o&#39;: 2, &#39;r&#39;: 1, &#39;t&#39;: 2, &#39;,&#39;: 1, &#39;I&#39;: 1, &#39;u&#39;: 1, &#39;p&#39;: 1, &#39;y&#39;: 1, &#39;n&#39;: 1}</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 简单序列计数</span>
sequece <span class="op">=</span> <span class="st">&#39;TCGAAATTCGGGCATGGAG&#39;</span>
count <span class="op">=</span> {}

<span class="cf">for</span> character <span class="op">in</span> sequece:
    count.setdefault(character, <span class="dv">0</span>)
    count[character] <span class="op">=</span> count[character] <span class="op">+</span> <span class="dv">1</span>
<span class="cf">for</span> k, v <span class="op">in</span> count.items():
    <span class="bu">print</span>(k <span class="op">+</span> <span class="st">&#39; &#39;</span> <span class="op">+</span> <span class="bu">str</span>(v))</code></pre></div>
<pre><code>T 4
C 3
G 7
A 5</code></pre>
</div>
</div>
<div id="section-1.18" class="section level2">
<h2><span class="header-section-number">1.18</span> 字符串操作</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#一个错误开始</span>
<span class="co">&#39;This is Bob&#39;</span>s book<span class="st">&#39;</span></code></pre></div>
<pre><code>  File &quot;&lt;ipython-input-26-9caaf6a74585&gt;&quot;, line 2
    &#39;This is Bob&#39;s book&#39;
                 ^
SyntaxError: invalid syntax</code></pre>
<p>双引号 字符串可以使用双引号，双引号之间可以使用单引号</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&quot;This is Bob&#39;s book&quot;</span> </code></pre></div>
<pre><code>&quot;This is Bob&#39;s book&quot;</code></pre>
<p>转义符 <code>\n</code>换行 <code>\'</code>单引号 <code>\&quot;</code>双引号 <code>\\</code>倒斜杠 <code>\t</code>制表符</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span>(<span class="st">&quot;Life is short,</span><span class="ch">\n</span><span class="st"> I use python&quot;</span>)</code></pre></div>
<pre><code>Life is short,
 I use python</code></pre>
<p>原始字符串： 忽略转义符号</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span>(<span class="vs">r&quot;Life is short,\n I use python&quot;</span>)</code></pre></div>
<pre><code>Life is short,\n I use python</code></pre>
<p>三重引号多行字符串</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span>(<span class="st">&#39;&#39;&#39;Life is short,</span><span class="ch">\n</span><span class="st"> I use python!</span>
<span class="st">do you like python</span>
<span class="st">yes&#39;&#39;&#39;</span>)</code></pre></div>
<pre><code>Life is short,
 I use python!
do you like python
yes</code></pre>
<p>多行注释“”&quot; “”&quot;</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&quot;&quot;&quot;</span>
<span class="co">简单序列计数</span>
<span class="co">多行注释</span>

<span class="co">&quot;&quot;&quot;</span>
sequece <span class="op">=</span> <span class="st">&#39;TCGAAATTCGGGCATGGAG&#39;</span>
count <span class="op">=</span> {}

<span class="cf">for</span> character <span class="op">in</span> sequece:
    count.setdefault(character, <span class="dv">0</span>)
    count[character] <span class="op">=</span> count[character] <span class="op">+</span> <span class="dv">1</span>
<span class="cf">for</span> k, v <span class="op">in</span> count.items():
    <span class="bu">print</span>(k <span class="op">+</span> <span class="st">&#39; &#39;</span> <span class="op">+</span> <span class="bu">str</span>(v))</code></pre></div>
<pre><code>T 4
C 3
G 7
A 5</code></pre>
<p>字符串切片</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="st">&quot;life is short, i use python&quot;</span>
a</code></pre></div>
<pre><code>&#39;life is short, i use python&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a[<span class="dv">1</span>:<span class="dv">5</span>]</code></pre></div>
<pre><code>&#39;ife &#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a[<span class="op">-</span><span class="dv">5</span>:<span class="op">-</span><span class="dv">3</span>]</code></pre></div>
<pre><code>&#39;yt&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a[<span class="op">-</span><span class="dv">3</span>:]</code></pre></div>
<pre><code>&#39;hon&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;python&#39;</span> <span class="op">in</span> a</code></pre></div>
<pre><code>True</code></pre>
<p>字符串方法</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="st">&#39;Hello world&#39;</span>
a.upper()</code></pre></div>
<pre><code>&#39;HELLO WORLD&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">b <span class="op">=</span> <span class="st">&#39;NI HAO&#39;</span>
b.lower()</code></pre></div>
<pre><code>&#39;ni hao&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">b.isupper()</code></pre></div>
<pre><code>True</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a.isupper()</code></pre></div>
<pre><code>False</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 简单介绍了验证码为什么不用区分大小写</span>
feeling <span class="op">=</span> <span class="bu">input</span>(<span class="st">&#39;How are you?</span><span class="ch">\n</span><span class="st">&#39;</span>)
<span class="cf">if</span> feeling.lower <span class="op">==</span> <span class="st">&#39;great&#39;</span>:
    <span class="bu">print</span>(<span class="st">&#39;I feel great too.&#39;</span>)
<span class="cf">else</span>:
    <span class="bu">print</span>(<span class="st">&#39;I hope you happy&#39;</span>)</code></pre></div>
<pre><code>How are you?
Great
I hope you happy</code></pre>
<p>is.X字符串方法 isalpha()是否只含字母 isalnum()是否只是字母或数字 isdecimal()是否只有数字 isspace()是否只有空格制表符换行 istitle()是否字符串为大写开头，后面均为小写字母</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="st">&#39;b1&#39;</span>
a.isalpha()</code></pre></div>
<pre><code>False</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="st">&#39;b c&#39;</span>
a.isalpha()</code></pre></div>
<pre><code>False</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="st">&#39;bc1&#39;</span>
a.isalnum()</code></pre></div>
<pre><code>True</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="st">&#39;1a&#39;</span>
a.isalnum()</code></pre></div>
<pre><code>True</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">while</span> <span class="va">True</span>:
    <span class="bu">print</span>(<span class="st">&#39;Enter your age:&#39;</span>)
    age <span class="op">=</span> <span class="bu">input</span>()
    <span class="cf">if</span> age.isdecimal():
        <span class="cf">break</span>
    <span class="bu">print</span>(<span class="st">&#39;Please enter a number for your age.&#39;</span>)

<span class="cf">while</span> <span class="va">True</span>:
    <span class="bu">print</span>(<span class="st">&#39;Select a new password (letters or numbers only):&#39;</span>)
    password <span class="op">=</span> <span class="bu">input</span>()
    <span class="cf">if</span> password.isalnum():
        <span class="cf">break</span>
    <span class="bu">print</span>(<span class="st">&#39;Passwords can only have letters or numbers.&#39;</span>)</code></pre></div>
<pre><code>Enter your age:
2
Select a new password (letters or numbers only):
+
Passwords can only have letters or numbers.
Select a new password (letters or numbers only):
2e</code></pre>
<p>startswith() 和 endswith()</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">seq <span class="op">=</span> <span class="st">&#39;&gt;0001</span><span class="ch">\n</span><span class="st">TCGATTACGG&#39;</span>
<span class="cf">if</span> seq.startswith(<span class="st">&#39;&gt;&#39;</span>):
    <span class="bu">print</span>(seq)</code></pre></div>
<pre><code>&gt;0001
TCGATTACGG</code></pre>
<p>join() 和 split()</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;,&#39;</span>.join([<span class="st">&#39;i&#39;</span>, <span class="st">&#39;love&#39;</span>, <span class="st">&#39;python&#39;</span>])</code></pre></div>
<pre><code>&#39;i,love,python&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;***&#39;</span>.join([<span class="st">&#39;i&#39;</span>, <span class="st">&#39;love&#39;</span>, <span class="st">&#39;python&#39;</span>])</code></pre></div>
<pre><code>&#39;i***love***python&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&quot;linux R perl C python&quot;</span>.split()</code></pre></div>
<pre><code>[&#39;linux&#39;, &#39;R&#39;, &#39;perl&#39;, &#39;C&#39;, &#39;python&#39;]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;i***love***python&#39;</span>.split(<span class="st">&#39;***&#39;</span>)</code></pre></div>
<pre><code>[&#39;i&#39;, &#39;love&#39;, &#39;python&#39;]</code></pre>
<p>文本对齐rjust() ljust() center()</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;hello&#39;</span>.rjust(<span class="dv">10</span>)</code></pre></div>
<pre><code>&#39;     hello&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;hello&#39;</span>.rjust(<span class="dv">20</span>,<span class="st">&#39;*&#39;</span>)</code></pre></div>
<pre><code>&#39;***************hello&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;hello&#39;</span>.center(<span class="dv">20</span>,<span class="st">&#39;-&#39;</span>)</code></pre></div>
<pre><code>&#39;-------hello--------&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> printPicnic(itemsDict, leftWidth, rightWidth):
    <span class="bu">print</span>(<span class="st">&#39;PICNIC ITEMS&#39;</span>.center(leftWidth <span class="op">+</span> rightWidth, <span class="st">&#39;-&#39;</span>))
    <span class="cf">for</span> k, v <span class="op">in</span> itemsDict.items():
        <span class="bu">print</span>(k.ljust(leftWidth, <span class="st">&#39;.&#39;</span>) <span class="op">+</span> <span class="bu">str</span>(v).rjust(rightWidth))


picnicItems <span class="op">=</span> {<span class="st">&#39;sandwiches&#39;</span>: <span class="dv">4</span>, <span class="st">&#39;apples&#39;</span>: <span class="dv">12</span>, <span class="st">&#39;cups&#39;</span>: <span class="dv">4</span>, <span class="st">&#39;cookies&#39;</span>: <span class="dv">8000</span>}
printPicnic(picnicItems, <span class="dv">12</span>, <span class="dv">5</span>)
printPicnic(picnicItems, <span class="dv">20</span>, <span class="dv">6</span>)</code></pre></div>
<pre><code>---PICNIC ITEMS--
sandwiches..    4
apples......   12
cups........    4
cookies..... 8000
-------PICNIC ITEMS-------
sandwiches..........     4
apples..............    12
cups................     4
cookies.............  8000</code></pre>
<p>strip(),rstrip(),lstrip() 删除空白字符</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">spam <span class="op">=</span> <span class="st">&#39;    hello    &#39;</span>
spam</code></pre></div>
<pre><code>&#39;    hello    &#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">spam.strip()</code></pre></div>
<pre><code>&#39;hello&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">spam.rstrip()</code></pre></div>
<pre><code>&#39;    hello&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">spam.lstrip()</code></pre></div>
<pre><code>&#39;hello    &#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="st">&#39;Hello world, welcome to python world&#39;</span>
a.strip(<span class="st">&#39;d&#39;</span>)</code></pre></div>
<pre><code>&#39;Hello world, welcome to python worl&#39;</code></pre>
<p>FINISH</p>

</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="index.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="py3-ct.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": false,
"twitter": false,
"google": false,
"weibo": false,
"instapper": false,
"vk": false,
"all": ["facebook", "google", "twitter", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"download": ["Py3_course.pdf"],
"toc": {
"collapse": "subsection"
}
});
});
</script>

</body>

</html>
